Exception Handling

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.
 
S

Sid Price

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.
 
A

Alvin Bruney [MVP]

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.
 
S

Sid Price

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.
 
K

Kevin Spencer

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
 
S

Sid Price

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.
 
K

Kevin Spencer

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
 
S

Sid Price

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top