PC Review


Reply
Thread Tools Rate Thread

Custom exceptions

 
 
=?Utf-8?B?dmJNZW50YWw=?=
Guest
Posts: n/a
 
      22nd Feb 2005
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)
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      22nd Feb 2005

>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/.../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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
=?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
Guest
Posts: n/a
 
      22nd Feb 2005
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!
***************************

"vbMental" wrote:

> 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)

 
Reply With Quote
 
=?Utf-8?B?dmJNZW50YWw=?=
Guest
Posts: n/a
 
      23rd Feb 2005
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.

"Cowboy (Gregory A. Beamer) - MVP" wrote:

> 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!
> ***************************
>
> "vbMental" wrote:
>
> > 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)

 
Reply With Quote
 
=?Utf-8?B?dmJNZW50YWw=?=
Guest
Posts: n/a
 
      23rd Feb 2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serializing Custom Exceptions jehugaleahsa@gmail.com Microsoft C# .NET 1 22nd Dec 2008 07:29 PM
Custom Exceptions Michael Moreno Microsoft C# .NET 1 7th Jun 2005 03:37 PM
Custom Exceptions with Web Services Chris Dunaway Microsoft ASP .NET 1 13th Jan 2004 11:35 PM
Question about Custom Exceptions Flare Microsoft C# .NET 6 10th Oct 2003 09:23 AM
Custom Exceptions in Remoting Matias Szulman Microsoft C# .NET 0 7th Oct 2003 09:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:10 AM.