Error handling issue: which one is better, multiple catches/throw or cast System.Exception to its ru

C

Carol

Exception may be thrown in the code inside the try block.
I want to handling the SqlException with State == 1 in a special way,
and for all others I want to use a general way to handle. Which of the
following options is better?
-----------------------------------------------------
Option 1:
try{...}
catch (System.Data.SqlClient.SqlException ex)
{
if (ex.State == 1)
{
// special error handling
}
else
throw ex;
}
catch (Exception ex)
{
// general error handling
}


--------------------------------------------------
Option 2:

try{...}
catch (Exception e)
{
if (ex.GetType() == System.Data.SqlClient.SqlException
&& ((System.Data.SqlClient.SqlException)ex).State ==
1)
{
// special error handling
}
else
{
// general error handling
}

}
 
T

Tom Porterfield

Carol said:
Exception may be thrown in the code inside the try block.
I want to handling the SqlException with State == 1 in a special way,
and for all others I want to use a general way to handle. Which of the
following options is better?
-----------------------------------------------------
Option 1:
try{...}
catch (System.Data.SqlClient.SqlException ex)
{
if (ex.State == 1)
{
// special error handling
}
else
throw ex;
}
catch (Exception ex)
{
// general error handling
}

If I read you correctly, this won't give you the desired result. A
throw from within a catch block will not cause the exception to be
caught again by another catch in the same try/catch. Instead the
exception will only be caught if there is a higher level try/catch than
what you show above, or if you wrap the code within the first catch in
its own try/catch.

What you might want to consider is placing your "general error handling"
in a separate method that call from either the 'else' that is in your
first catch, or from your second catch.
--------------------------------------------------
Option 2:

try{...}
catch (Exception e)
{
if (ex.GetType() == System.Data.SqlClient.SqlException
&& ((System.Data.SqlClient.SqlException)ex).State ==
1)
{
// special error handling
}
else
{
// general error handling
}

}

I would do this slightly different (mostly a preference thing), but the
above will basically give you what you want.
 
C

Carol

Thank you, you are absolutely right:)

If I read you correctly, this won't give you the desired result. A
throw from within a catch block will not cause the exception to be
caught again by another catch in the same try/catch. Instead the
exception will only be caught if there is a higher level try/catch than
what you show above, or if you wrap the code within the first catch in
its own try/catch.

What you might want to consider is placing your "general error handling"
in a separate method that call from either the 'else' that is in your
first catch, or from your second catch.







I would do this slightly different (mostly a preference thing), but the
above will basically give you what you want.
--
Tom Porterfield- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
 

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