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