PC Review


Reply
Thread Tools Rate Thread

adding info to an exception

 
 
Jeff
Guest
Posts: n/a
 
      13th Nov 2007
hey

..NET 2.0

I'm wondering if tit's possible to add some info to an exception. lets say I
want to add "helloworld" to an exception so where it's caught can check for
this "helloword" text...

The reason I ask is that I'm programming a n-tier application and want
database errors to be caught in the data layer. but giving the user a
another errror. The user errortext is determined in the presentationlevel.
So when for exampel an error occur in the data layer the text "invalid
column" is written to the eventlog and the user get to show another error.

I know I can in the catch block throw another exception but that is not a
good solution....

any suggestions


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      13th Nov 2007
Jeff,

That's about the only solution you have, really. Why is it not a good
solution?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Jeff" <(E-Mail Removed)> wrote in message
news:OCj%(E-Mail Removed)...
> hey
>
> .NET 2.0
>
> I'm wondering if tit's possible to add some info to an exception. lets say
> I want to add "helloworld" to an exception so where it's caught can check
> for this "helloword" text...
>
> The reason I ask is that I'm programming a n-tier application and want
> database errors to be caught in the data layer. but giving the user a
> another errror. The user errortext is determined in the presentationlevel.
> So when for exampel an error occur in the data layer the text "invalid
> column" is written to the eventlog and the user get to show another
> error.
>
> I know I can in the catch block throw another exception but that is not a
> good solution....
>
> any suggestions
>
>



 
Reply With Quote
 
Olie
Guest
Posts: n/a
 
      13th Nov 2007
Throwing another exception is exactly what you are supposed to do. You
have probably read that re-throwing an exception is not recommended
for performance reasons. That refers to re-throwing the same
exception, it is perfectly OK to transform the exception into another
more descriptive exception and throw that.

The whole exception model is built around the capability to add more
information as the exception propagates down the chain. That is why
you have the InnerException property to allow you to build up
exceptions with more information.

The best method is to declare your own Exception class that inherits
from ApplicationException. When you call the constructor you pass the
old exception in along with your new more descriptive error message.
You can even add extra properties to store more information so you
could have a short and a long error message if you wanted. Then just
throw this exception.




 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      13th Nov 2007
On Nov 13, 4:29 pm, Olie <owa...@gmail.com> wrote:
> Throwing another exception is exactly what you are supposed to do. You
> have probably read that re-throwing an exception is not recommended
> for performance reasons. That refers to re-throwing the same
> exception, it is perfectly OK to transform the exception into another
> more descriptive exception and throw that.


I have no problem with rethrowing the same exception, if you need to
do some logging or something else before rethrowing. If you need to
add more information, of course, using an InnerException of the
original is a good plan as you say.

> The whole exception model is built around the capability to add more
> information as the exception propagates down the chain. That is why
> you have the InnerException property to allow you to build up
> exceptions with more information.
>
> The best method is to declare your own Exception class that inherits
> from ApplicationException.


Deriving from ApplicationException is discouraged these days - it
doesn't add anything useful, and just makes the inheritance chain
longer for no good reason. I'd just derive from Exception.

Jon

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      13th Nov 2007
Jeff,
In Framework 2.0 and beyond, the Exception class has a Data property which
can contain name / value information. You can populate this with whatever
name=value items you want, and pick it up when the exception is processed /
handled farther up the call stack.
-- Peter
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com



"Jeff" wrote:

> hey
>
> ..NET 2.0
>
> I'm wondering if tit's possible to add some info to an exception. lets say I
> want to add "helloworld" to an exception so where it's caught can check for
> this "helloword" text...
>
> The reason I ask is that I'm programming a n-tier application and want
> database errors to be caught in the data layer. but giving the user a
> another errror. The user errortext is determined in the presentationlevel.
> So when for exampel an error occur in the data layer the text "invalid
> column" is written to the eventlog and the user get to show another error.
>
> I know I can in the catch block throw another exception but that is not a
> good solution....
>
> any suggestions
>
>
>

 
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
Exception trhown during Catching... How to get both exception Info? Paolo Microsoft C# .NET 2 23rd Jan 2007 11:53 AM
Info on unhandled exception during break mode, no exception variable? Samuel R. Neff Microsoft VB .NET 5 13th Dec 2004 07:47 PM
Info on unhandled exception during break mode, no exception variable? Samuel R. Neff Microsoft VB .NET 0 16th Nov 2004 07:39 PM
Adding Custom Info to an Exception Raterus Microsoft ASP .NET 4 22nd Sep 2004 01:27 PM
Getting more info from exception Claire Microsoft C# .NET 3 19th May 2004 06:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:32 AM.