> If Module A call Module B and Module B throw and Exception (throw new
Exception("There are problems in Module B"). If Module A wanted to
throw and exception and just pass on the text of Module B exception,
would it be coded like "throw new Exception(e.Message)"?
If Module A wanted to throw it's own exception with its own text, how
would module which called Module A find the text for both Module A and
Module b?
Thanks in advance for your assistance.
Look up the constructor of the Exception class and find the overload that
has "innerException", that's the one you want.
Simply told, you can construct a new Exception object and pass it an "inner"
exception, and the code handling the exception can look at the InnerException
property to get access to it. And since each Exception object can have an
inner one, you can nest these to any level.
thus, to rethrow a new exception and preserve the old one you could use something
like this:
catch (SomeException ex)
{
throw new DifferentException("Your code screwed up", ex);
}
However, make sure there is a valid reason for doing this. If rethrowing
as a new exception gives the code that handles this new one more information
than what the previous SomeException would give, then it's a good idea, otherwise
you might just add code for no reason.
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2