Exception Handling

  • Thread starter Thread starter Sid Price
  • Start date Start date
S

Sid Price

I have a third party class library (C#) that I am expanding and using with
an application (VB.NET) and I am having trouble catching exceptions in the
application. The class library has an exception handler that it calls:

catch (Exception ex)

{

_sSessionId = null;

objUtils.HandleException("Error: Could not login: " + ex.Message);

return "";

}

The handler then throws an exception (the one my application should catch):

throw new Exception(Environment.NewLine + strMessage);

My application has a typical try/catch block but it does not catch the
thrown exception; here Login is in the class library:

Try

strSessionID = oVR.Login()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

I am pretty new to exception handling so perhaps I am misunderstanding, can
someone point me in the right direction please?

Sid.
 
Thank you for the Blog URL, interesting reading.

I have the source for the class library and the code clips I included were
directly from that source. What could they be doing wrong?
Basically in a method of the class library on an exception they call an
exception handler, that handler then throws an exception that my application
fails to catch. Looking at a call stack:
MyApplication ----> Method in Library ---> On an exception it calls an
exception handler ---> throws an exception (not caught by MyApplication.
Is this breaking the rules? Should I change the library to simply throw
exceptions that my application should catch?
Thanks again,
Sid.
 
No you shouldn't. Your code should catch the exception. How do you know it
isn't catching the exception? Put a break point inside the handler and run
the application. It should catch.
 
If I put a breakpoint in the handler of the class library it gets hit,
however, a breakpoint where my application should catch the exception thrown
by the handler is never hit.
Sid.
 
It sounds like something between the exception being thrown in the class
library and the application's use of it is catching the exception.
Therefore, it doesn't bubble up to the application.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
Kevin,
Thanks for the reply; is it okay that the class library to throw an
exception that is caught with its own handler and then for that handler to
throw another exception for the user to catch? Nested exceptions being
thrown.
Sid.
 
Not a problem at all, if used correctly. A class library should throw
exceptions unless it can handle them gracefully. My class libraries include
exception handlers that log the details of the exceptions and then throw
them routinely. The logging is optional, and can be turned off if desired,
but I find it very useful in diagnosing problems.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
What is describe is exactly what I am trying to achieve, if only I could
figure out why my application is not catching the exceptions,
Sid.
 
Back
Top