Custom exceptions

G

Guest

Following guidelines from MSDN, I created my custom exceptions by inheriting
from ApplicationException. Unlike the .NET framework exceptions, I cannot set
my Message property in the first line of the constructor like this:

Class CustomException(Message as string)
MyBase.New(Message)
End Class

I cannot do this because I need to do some other stuff before I calculate
the Message property. But after that line right there, there is no way to set
the Message property.
I tried overriding the ToString() property like this:

Overrides Function ToString() as string
Return "My custom message"
End Function

This doesn't work, when use the "Throw" statement on an instance of my
CustomException. Instead the exception window just says "Error in the
application".

Any ideas on what I am doing wrong? (TIA)
 
M

Mattias Sjögren

Following guidelines from MSDN, I created my custom exceptions by inheriting
from ApplicationException.

Then you may want to read this

http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx


Class CustomException(Message as string)
MyBase.New(Message)
End Class

I cannot do this because I need to do some other stuff before I calculate
the Message property. But after that line right there, there is no way to set
the Message property.

You can put the calculations in a shared method and call it as part of
the call to the base constructor.

MyBase.New(CalculateStuff(Message))
....

Shared Function CalculateStuff(message As String) As String



Mattias
 
G

Guest

Not sure what you are doing, but I cannot see a reason why you cannot
calculate the information before instantiating the Exception object.
Something like:

Try
'Try some stuff
Catch
'Oops a problem happened here, so lets calculate some stuff
'Now we are finished, let's throw our custom exception
End Try

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
G

Guest

You're right, I could calculate before throwing the custom exception but in
that case I might as well be simply throwing an ApplicationException and
setting the message in the constructor. In other words, that would make my
CustomException useless except for it's type.
What is the value (as developers) for us to inherit from the
ApplicationException class as stated if you are limited to defining the
message in the first line of the constructor?

I could be overstating the problem, but here's what I had in mind:

Creating an exception class that was more user friendly by me passing it
some parameters such as the business process that was attempting to be
performed, what went wrong (in terms simpler than the exception.message
property that caused), and what could possibly done to resolve it... I wanted
to pass in those properties (sort-of) and have it displayed in the message
property, but i'm afraid that if the dialog that pops up when I use the THROW
statement wont show the message (as I have overridden it - it only shows
"Error in the application.") then I'm afraid that my more 'helpful' message
would be surpressed.
 
G

Guest

After reading that blog, atleast I know I'm not the only one who fails to
find a good use for that class. I'll use exception until I have a good reason
not to.
 

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

Top