Adding to Exception ?

  • Thread starter Thread starter exBK
  • Start date Start date
E

exBK

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.
 
exBK said:
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.
 
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.
 
Thanks for the reply. Any example of how to do it ? I am not sure what you
meant by "exception type" ...
 
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.
 
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 said:
Thanks for the reply. Any example of how to do it ? I am not sure what you
meant by "exception type" ...
 
inline
Jon Skeet said:
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.
 
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 said:
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;
}
 
Back
Top