>
How can I catch an error and throw the exception message plus some custom text?
<Snip>
VMI,
You can throw an exception with a custom error message without writing a
custom exception class. Inside your catch block, just throw the most
appropriate pre-defined exception type with your modified error message.
Note, it is generally considered best practice to throw a fairly specific
exception instead of the generic System.Exception. Let's say if you have a
stub method that is not supported yet, you should throw a
NotSupportedException. Or if you are validating inputs inside a function
call you might throw a InvalidArgument exception. Otherwise you might throw
an ApplicationException.
Here is an example.
sbyte i = 127;
try
{
// Generates an overflow error. Signed byte can only hold
values from -128 to 127..
checked {
i = (sbyte) (i+1);
}
}
catch (Exception ex)
{
// Throw an exception with a modified error message.
throw new ApplicationException("Something went wrong here: " +
ex.Message);
}
Useful links for exception handling guidelines:
http://www.codeproject.com/dotnet/ex...nceperthread12 http://msdn2.microsoft.com/en-us/lib...87(VS.71).aspx
Hope this helps,
Jason Vermillion