Need help with ApplicationException

G

Guest

I need to find a way to share information between two classes, one is an
employee class and the other is a custom error class that inherits from
ApplicationException. These two classes are part of a ClassLibrary project.
The other project has all the web page classes.

My project allows employees to log in using Forms Auth, I do some checking
on the person that is logging in and then after that is good I create an
employee object and that is stored in a session variable.

What I would like is if this employee that logged in gets an error message
at anytime during their use of the web site that the error gets logged with
their userid name. I can log errors and all that, I am logging them to a
database. What I need is just how do I get the error message with the userid
name.

Thanks,
Charles
 
K

Kevin Spencer

Okay, I'm assuming that somewhere in this employee object stored in a
session variable is the "userid name." If so, and you're creating your own
custom Exception class (why are you inheriting ApplicationException instead
of Exception?), you can create a field named "userid" in the custom
Exception class, and implement a constructor method that includes the
"userid" as a parameter. Then in your Try/Catch (you ARE using Try/Catch,
aren't you???), you do something like the following:

catch (Exception ex)
{
throw new MyCustomException(ex.Message, ex,
((employee)Session["CurrentEmployee"]).userid);
}

Of course, what you do with your new Custom Exception is up to you, but I
hope you get the idea.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.
 
G

Guest

Thank you for your reply!

Yes there is a "userid name" variable in the employee object.

I am not sure why I am using ApplicationException instead of Exception. The
only reason that I do have is that I found it on an example. Why should I be
using Exception and not ApplicationException?

I wanted to try to avoid implementing a constructor method that includes the
"userid" as a parameter and having other programmers remember how to add the
userid to a Throw New line. But, if there is no other way to do it besides
the way that you explined that I guess I am stuck doing it that way. I am
using a Try/Catch block.

I was hoping for something along these lines,
Public Class InvalidRetrieveException
Inherits ApplicationException

Public Sub New()
MyBase.New()

End Sub


Public Sub New(ByVal message As String, ByVal InnerException As
Exception))
'_userName comes from Employee object (somehow)
MyBase.New(message & ": " & _userName, InnerException)
WriteToLog(message & ": " & _userName, InnerException)
End Sub

End Class

Thanks again,
Charles

Kevin Spencer said:
Okay, I'm assuming that somewhere in this employee object stored in a
session variable is the "userid name." If so, and you're creating your own
custom Exception class (why are you inheriting ApplicationException instead
of Exception?), you can create a field named "userid" in the custom
Exception class, and implement a constructor method that includes the
"userid" as a parameter. Then in your Try/Catch (you ARE using Try/Catch,
aren't you???), you do something like the following:

catch (Exception ex)
{
throw new MyCustomException(ex.Message, ex,
((employee)Session["CurrentEmployee"]).userid);
}

Of course, what you do with your new Custom Exception is up to you, but I
hope you get the idea.
--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

Charles said:
I need to find a way to share information between two classes, one is an
employee class and the other is a custom error class that inherits from
ApplicationException. These two classes are part of a ClassLibrary
project.
The other project has all the web page classes.

My project allows employees to log in using Forms Auth, I do some checking
on the person that is logging in and then after that is good I create an
employee object and that is stored in a session variable.

What I would like is if this employee that logged in gets an error message
at anytime during their use of the web site that the error gets logged
with
their userid name. I can log errors and all that, I am logging them to a
database. What I need is just how do I get the error message with the
userid
name.

Thanks,
Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top