Hello,
I would like to understand how it is possible when coding in C# to know
which type of exceptions can be thrown by some code placed on a
try/catch expression, so I can specify in the catch parameters which
type of exception I want to catch ?
I have to say this point has always been obscure to me ...
Thank you for your help.
Looks to me like you could use some code examples of how to throw and
catch different exceptions:
try
{
// Do some stuff.
...
if (AppErr)
{
// Throw an appliation exception with a message that can be
retreived from the Exception.Message method.
throw (new ApplicationException("This is an Application
Exception msg that occurred because of blah, blah"));
}
else if (GeneralErr)
{
// Throw a more general exception
throw (new SystemException("This is a System Exception msg
that occurred because of blah, blah, blah"));
}
}
catch (ApplicationException)
{
// Get the type of the exeception from the Exception object.
String S = "Application Exception type = " +
ex.GetType().ToString();
DispUIMsg(S);
// Get the message associated with the exception.
S = "The reason for the error is because of = " ex.Message;
DispUIMsg(S);
}
catch (System.Exception ex)
{
// Get the type of the exeception from the Exception object.
String S = "System Exception type = " + ex.GetType().ToString();
DispUIMsg(S);
// Get the message associated with the exception.
S = "The reason for the error is because of = " ex.Message;
DispUIMsg(S);
}
catch
{
// This is the last ditch error handler. Most of the time
exceptions should be caught by one of the exception
// handlers above. If they don't catch it, then this one should.
But, there is no message information.
String S = "Problem - an unknown exception has been thrown.";
DispUIMsg(S);
}
When debugging, you can tell the debugger to catch an exception when
it is thrown. This is handy to help track down bugs from exceptions
right after they are thrown rather than having it wonder up the call
stack looking for an exception handler. Exception settings are
available from the Debug / Exceptions menu item from Visual Studio
Bob Bryan MCSD
Please visit my blog:
http://designingefficientsoftware.wordpress.com