throwing exception to application.....if there's any better way to do it??

F

Farooq Khan

i'm writing a class library having following code snippet. i want this class
to report an error (by throwing an Exception) to the application using this
class library. the problem is that within that try block there are several
exceptions that this class itself needs to handle (interrnally). now when
the exception, UserAlreadyRegistered, is thrown the class' catch block
(within library) catches it and no exception is thrown to the application
further using it.
for this in catch block i have put up another throw instruction if the
exception occured was "UserAlreadyRegistered" in order to pass the exception
to the application.
let me know if there's some better way to do it....and plus what is
"InnerException" supposed to do????

try
{

if (this.IsRegistered(userInfo.EmailAddress))
throw new Exception("UserAlreadyRegistered"); // the exception
supposed to be caught by application using this class
// code goes on

}
catch (Exception exp)
{

if (exp.Message=="UserAlreadyRegistered")
throw exp;
// some exception handler

}


thanks.

Farooq Khan
 
J

Jon Skeet [C# MVP]

Farooq Khan said:
i'm writing a class library having following code snippet. i want this class
to report an error (by throwing an Exception) to the application using this
class library. the problem is that within that try block there are several
exceptions that this class itself needs to handle (interrnally). now when
the exception, UserAlreadyRegistered, is thrown the class' catch block
(within library) catches it and no exception is thrown to the application
further using it.

Only catch the *types* of exception you really want to handle, and
throw a specific UserAlreadyRegisteredException type. Throwing a bare
Exception is a bad idea - it means you can't easily catch specific
exceptions, as you've found in your own code.
 

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