tags:

views:

529

answers:

8

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...

+5  A: 

In my opinion it doesn't have to be. Sometimes you want some code within the first try to execute even if the code in the second one fails.

Might also add that other people are right in what they're saying also. I'm just saying it doesnt have to be bad always. In a program that's not demanding performancewise you could just do it whatever way you like.

Jonas B
A: 

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 )
{
}
Nissan Fan
Why did this get downvoted? +1 to compensate.
Alex
"Everytime you do a try-catch you're creating another thread." I don't think this is correct.
Ryan Michela
@Ryan Michela: It's not correct, which is why I downvoted.
Ben S
there's no correlation between try-catch and number of threads. [note: I'm *not* the one who downvoted]
Isak Savo
"Creating another thread"? That is not true. (That's where the DV came from)
Rex M
I didn't downvote, but I didn't upvote based on the thread-creation bit. I've never heard anything like that before.
overslacked
@Alex: you should never "upvote to compensate", it was downvoted because it was wrong, and hopefully the author will realize that and delete it.
Neil N
I have no idea what you mean it's wrong. Everytime you do a try catch within another try-catch you are threading more and more statements together. The point is readability is encumbered. Not Thread. Had I meant Thread I would have said Thread. Coming from a group of people who use a case sensitive language I'm real surprised.
Nissan Fan
@Nissan Fan: I understand your meaning, here, but "thread" is a poor choice of terminology - "thread" has a specific meaning, which isn't what you're stating. Every time you add a try/catch, you're adding another "instruction path" or "execution path" would have been better, in this case. If you say thread, you're suggesting multi-threading, and it doesn't matter whether you say "thread" or "Thread" - both have hte same meaning to most programmers.
Reed Copsey
@Nissan sorry you were misunderstood. Please realize pretty much all developers use "thread" to mean one thing: http://en.wikipedia.org/wiki/Thread_%28computer_science%29 ... using it in another context clearly causes confusion.
Rex M
Sorry, biggee. I'm very familiar with threading, I just should have not been so terse in my answer.
Nissan Fan
+3  A: 

It depends on what your program needs to do. It's generally best to wrap your try/catch around the smallest scope of work possible and focus your exception handling as narrowly as possible to avoid unexpected behavior, side effects or bugs. Many times this will mean having a sequence of try/catch blocks, which is fine.

Rex M
@anon why the DV?
Rex M
+1 Downvote compensated for...
Chris Ballance
+17  A: 

No. I don't think this is bad practice at all.

If you are doing something in the first, nested try which you can catch and handle properly, this is perfectly fine, especially if you're "catching" different types of exceptions in the two handlers.

However, I would say that this is potentially a good opportunity for refactoring, and splitting the nested section into a separate method. Often, when I've seen this, it's a sign that the method should be split into smaller methods. However, this is not always true.

Reed Copsey
+1 for refactoring/separating the nested try/catch blocks as their own methods.
JamesEggers
Totally agree +1 for refactoring... Will implement this...
JL
+1 for good insight and explanation
Jonas B
Glad you got the upvote, but some dillweeds downvoted me for no reason because they don't understand the English language.
Nissan Fan
Heh, I got a downvote, too. That's, unfortunately, part of the SO experience. I wish you had to comment to downvote, sometimes, since I"d like to know why people disagree.
Reed Copsey
+1  A: 

I think it depends on how you are handling the inner exception. It may be very logical that your inner catch there needs to do something completely different than would fall int the scope of the outer catch. The one thing you would want to avoid is hiding the inner exception as being the cause of the outer exception. In other words, I would highly recommend

try
{ 
     // Do something 
     try
     {
         // Do something
     }
     catch(MyException e)
     {
         // handle
         throw; // <--- This is important, it rethrows the same exception while maintaining the stack trace.  This is *different* from "throw e"
     }
}
catch(AnotherException e)
{
    // handle 
}
Matt
+1 for pointing out a nuance of throw.
Ryan Michela
throwing exceptions like this is perfomance consuming...
astander
@astander - it's performance consuming if and only if an exception is actually thrown. And since exception are, after all, exceptional, this shouldn't be a problem in most code.
Matt
That is true, but if handle correctly, such cases should happen very seldom yes, so i would recomend handling the exception by checking before accepting exception coverage as the altimate resort. Check your params, check your files exist, check you have the object in the collection before assuming
astander
A: 

It's not bad practice, so long as the logic warrants it.

For example, if the outer-most try is for catching a SQL exception, and the inner-most try is for an IO exception, it might be reasonable to do this.

However, be careful to avoid having catch-all try clauses that cover a large number of lines of code. Those are not good practice.

Ben S
A: 

No, it is not necessarily bad form.

Just like most of these types of questions (ie. Is goto evil?, Is for(;;) evil?) these things have their place and are ok if used when they are needed.

Dana Holt
A: 

If there's an exception you can handle inside the first try block and still continue on successfully, I don't think there's any problem with it. If it's just to do something and re-throw, just add another catch block for the specific type of exception you are looking for and handle it there.

Nick