Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

like image 241
JL. Avatar asked Sep 21 '09 18:09

JL.


People also ask

Is it bad practice to have nested try catch?

Although this is sometimes unavailable, nesting try/catch blocks severely impacts the readability of the source code as it makes it difficult to understand which block will catch which exception.

Is it a good practice to use try catch?

Don't Overuse the “Try-Catch” The first best practice of “Exception Handling” is, don't overuse “Exception Handling.” Typically, we handle exceptions at the outer layers and throws from the inner so that once an exception occurs, we can better understand what leads to it.

Is try catch bad for performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Is it bad practice to catch exception?

Also when you catch all exceptions, you may get an exception that cannot deal with and prevent code that is upper in the stack to handle it properly. The general principal is to catch the most specific type you can. catch(Exception) is a bad practice because it catches all RuntimeException (unchecked exception) too.


3 Answers

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.

like image 187
Reed Copsey Avatar answered Oct 22 '22 01:10

Reed Copsey


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.

like image 27
Jonas B Avatar answered Oct 22 '22 02:10

Jonas B


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.

like image 3
Rex M Avatar answered Oct 22 '22 01:10

Rex M