C# exceptions

  • Thread starter Thread starter Ghost
  • Start date Start date
G

Ghost

It there any way to attatch the point of creation of an
error which is thrown in an object to the instance that
caused it to be thrown rather than the throw statement?
 
I don't know whether I understand your question.

If you are talking about re-throwing an exception, the exception class does
have an InnerException property that you can use to keep a reference to the
original statment. Also, the exception includes a string representation of
the call stack.
 
I think I know what you mean. ;-)

You need to create an additional constructor for your custom exception class
which also contains the inner exception, and calls its base class
constructor with the same signature:

I.e.:

public MyException(string message, Exception inner) : base(message, inner)
{}

When you then throw your custom exception make sure you use this constructor
and put the original Exception in as 2nd parameter. The calling app can then
retrieve the full StackTrace from your inner exception.

Hope that makes sense somehow. :-)

Cheers,
Wim Hollebrandse
 
Back
Top