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...
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With