By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Adding to Exception ?

P: n/a
Hi,
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do
that ??
Thanks for your time.

Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do
that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Couldn't you just
throw new System.Exception(ID, e);

Then again, you wouldn't know at run time what kind of exception this was.
So ...
Depends on what you need I suppose.

--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #3

P: n/a
Thanks for the reply. Any example of how to do it ? I am not sure what you
meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

P: n/a
The below is from MSDN.
http://msdn.microsoft.com/library/de...exceptions.asp

You could add your Property to hold the your invoice number.

using System;
public class EmployeeListNotFoundException: ApplicationException
{
public EmployeeListNotFoundException()
{
}
public EmployeeListNotFoundException(string message)
: base(message)
{
}
public EmployeeListNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}

Nov 15 '05 #5

P: n/a
You could do this, but the ID would only be part of the message. That means
that if some code wanted to know the ID, they would have to parse the
message. Parsing messages is usually a bad idea as you are then dependent
on the format of the message.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...
Couldn't you just
throw new System.Exception(ID, e);

Then again, you wouldn't know at run time what kind of exception this was.
So ...
Depends on what you need I suppose.

--
The hotmail account will most likely not be read, so please respond only
to the news group.

Nov 15 '05 #6

P: n/a
public class NewException : Exception{
public NewException(string message, int id)
: base(message){
__theId = id;
}

public int Id{
get{return __theId;}
set{__theId = value;}
}

private int __theId;
}

"exBK" <ex**@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Thanks for the reply. Any example of how to do it ? I am not sure what you meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #7

P: n/a
inline
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.


Small point. That depends if he is catching some other type in a catch
handler, wrapping it in a new exception (his own custom exception object),
and then thowing the new type from the catch handler, or simply creating a
new exception of this own type anywhere in the code and throwing it.

This is the "catch-wrap-throw" technique, which can only be done from a
catch handler, versus the "throw new type" technique, which can be done
anywhere.

Another small point. His example implies that he is retrieving the exception
from a server. If this is the case then defining a custom type requires that
the assembly that contains the exception type definition be distributed so
that both client and server have access to the same assembly. Otherwise when
the exception is deserialzed on the client it will throw an exception (I
forget which one) which will mask the "real" exception.

His code shows he was accessing the innerexception property to ascertain the
cause. This is almost always a bad idea - he should access the outer
exception for the error reason and examine the inner exception(s) more for
documentation purposes then for programmatic action.

Nov 15 '05 #8

P: n/a
Just a small side note.
You should derive from ApplicationException and not directly from Exception.
This is a kind of "best practice" given by MS

Josť
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...
public class NewException : Exception{
public NewException(string message, int id)
: base(message){
__theId = id;
}

public int Id{
get{return __theId;}
set{__theId = value;}
}

private int __theId;
}

"exBK" <ex**@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Thanks for the reply. Any example of how to do it ? I am not sure what

you
meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
> I have an exception handler which captures the exception.
> Exception e = Server.GetLastError().InnerException;
>
> I want to add an identifying ID (such as Invoice No) to "e". How do I
do
> that ??

The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains

the original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.