Alex Rivera | Logout

Is it bad practice to nest 2 try catch statements in C#?

Asked 2009-09-21T18:15:57.893
14

Is the following code bad practice?

 try //Try Overall Operation
      {
           try //Try section 1 of operation
               {

               }
           catch(exception ex)
               {
                    //handle exception code
                    //throw the exception
       }
 catch (exception ex)
      {
          // send soap exception back to SOAP client.
      }

I know from a program review point of view, other developers seeing 2 tries nested directly like that might wonder why, but is it totally taboo, or is it accepted practice now days?

Thanks Guys, I agree with you all about the refactoring, going to create a seperate method for the sub functionality, the method was getting really long. I am very impressed to all of you who picked this up...

c#
Edit
Report

1 Answer

-4

It's not terrible, however see if you can't just solve this by handling the Exception types as necessary instead:

try
{

} 
catch (SqlException ex )
{
// Catches specific exception
}
catch ( Exception ex )
{
// Catch-all
}

Everytime you do a try-catch you're creating another thread of statements that encumbers readabilitiy. E.g.:

try
{
   try
   {
   }
   catch ( Exception ex )
   {
   }
}
catch ( Exception ex )
{
}
answered 2009-09-21T18:19:21.947

Your Answer