By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Farooq Khan <fa*********@pk.softechww.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.