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

  • Thread starter Thread starter Farooq Khan
  • Start date Start date
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
 
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.
 
Back
Top