views:

309

answers:

10

Is it considered bad practice to structure the code like this?

public void whatever() {
   try {
     methodThatMayThrowIOException();
   } catch(IOException io) {
     // do something with exception here
   }

   // do more stuff here that won't throw exceptions

   try {
     methodThatMayThrowCustomException();
   } catch(CustomException ce) {
     // do something with custom exception here
   }
}
+3  A: 

Looks OK but it depends on what methodThatMayThrowIOException and methodThatMayThrowCustomException do and whether failure of the first one (methodThatMayThrowIOException) should fail the whole whatever method.

cherouvim
+2  A: 

I don't see why that would be bad practice, as long as you actually do something useful with the exceptions you caught.

extraneon
+7  A: 

If the method can continue execution, without any problems even if the specified exception occurs this should be fine.

In the event where the exception should cause problems further down the line in the method, I would have it bubble up.

astander
+4  A: 

No it's not. It is a good point to catch specific exceptions when they might be thrown, imho it is surely better than this:

public void whatever {
    try {
        methodThatMayThrowIOException();
        // do more stuff here that won't throw exceptions
        methodThatMayThrowCustomException();
    } catch(ParentException e) {
        // do something with custom exception here
    }
}
Alberto Zaccagni
How is this worse if methodThatMayThrowCustomException() can work perfectly fine after methodThatMayThrowIOException() threw an exception, which was handled?
Thomas Lötzer
Besides, in the example, it's not sure at all that IOException and CustomException haver a common parent (except Exception, but catching Exception would not be a great idea)
Valentin Rocher
@Thomas Lötzer: what? `methodThatMayThrowCustomException()` is not going to be executed at all if `methodThatMayThrowIOException()` throws an exception... If you are talking about a try catch inside the method then you're making assumptions, that's out of the scope of the question I think.@Valentin Rocher: Not catching a common parent (even if that's only Exception), is *exactly* the point of my answer.
Alberto Zaccagni
Ok, I misread your answer. Could you make a dummy edit so I remove the downvote ?
Valentin Rocher
ok I made a dummy edit myself, and removed the downvote. Sorry !
Valentin Rocher
@MontecristoAnd that is exactly the point I was making. It is impossible to answer the question without knowing more. Your answer may be right or wrong, depending on the circumstances: If the IOException can be safely handled so that the rest of the method can be executed and do what it is supposed to, then the original version is much better than your "surely better" version.
Thomas Lötzer
@Thomas Lötzer: Geo clarified it before: "The IOException was just an example. I was wondering about multiple try's in one method.", so I don't think that we're dealing with try catches inside those two methods... btw, of course you might be right ^^
Alberto Zaccagni
A: 

well it depends on what you are trying to accomplish. you would normally enclose a block of code in a single try-catch block if the code block should be executed as a block or not at all if some exception occurs. if it's not the case, then i believe it is much better to isolate the different code blocks throwing different exceptions into different try-catch blocks sandwitching "do more stuff here that does not throw exception". just a thought!

ultrajohn
+3  A: 

It really depends on what you plan to do if an IOException thrown. Your style allows you to do more things, but if you're not actually planning to take advantage of that, then that intention is made much clearer by using the standard idiom.

public void whatever {
   try {
     methodThatMayThrowIOException();
     // do more stuff here that won't throw exceptions
     methodThatMayThrowCustomException();
   } catch(IOException io) {
     // do something with io exception here
   } catch(CustomException ce) {
     // do something with custom exception here
   }
}

Here you can tell quickly that if IOException is thrown, then you only do what's inside the catch block and not much else.

polygenelubricants
He is ALREADY taking advantage of that by having a 2nd and third section.
Yar
Yes, but the first catch block could return/throw, or it could set guard flags that prevent the actual meat and potatoes of the 2nd and 3rd section from executing, etc.
polygenelubricants
True. I kind of assumed he was asking "my code does exactly what I want. Is this is a bad idea?" But you're right. It may not be doing exactly what he wants. +1
Yar
+1  A: 

There is no problem with this, AFAICS. However, 2 try-catch in a method stabs in my eyes. If you feel the same, I suggest you to refactor it in a proper way.

Adeel Ansari
+1  A: 

No. That's a pretty good practice to narrow down the scope where some kind of exceptions are thrown. I did this a lot in my code.

However, if you are sure that in one try...catch block, certain kind of Exception will only be thrown by a unique function, putting them in the same try block is also OK.

evanmeng
A: 

Personally, I think it looks cluttered. I prefer to have just one try, with as many catch blocks as I need. I don't care or multiple try/catch sequences in a single method.

duffymo
+1  A: 

Your code reads like it does because you want to do part 1 (and resolve, catching IOException if necessary), do the no-exceptions part, and then do the methodThatMayThrowCustomException. Your code can literally not be written any other way and retain the same functionality. That's an exaggeration, but any other version would be different in a superficial sense.

This is not the same:

public void whatever {
   try {
     methodThatMayThrowIOException();
     // do more stuff here that won't throw exceptions
     methodThatMayThrowCustomException();
   } catch(IOException io) {
     // do something with io exception here
   } catch(CustomException ce) {
     // do something with custom exception here
   }
}

and the way it would execute if any of the Exceptions are thrown is quite different. If you need to sequentially recover from Part 1, hit Part 2 in any case, then carry on to Part 3, you cannot really write your code any other way.

There's nothing wrong with having two catch blocks, though mixing something that causes an IOException with something that throws a CustomException might suggest a mixing of concerns, making your code hard to understand. But as it is, it's not just valid, it's the only way to do what you're doing.

Yar
He never explicitly said that he wants to hit part 2 in any case. The `//do something with io exception here` part could be `throw new WhateverException();`, for example, in which case the multiple catch blocks would do just fine and is much clearer. That's the point I'm trying to make in my answer: only use his approach if he's actually taking advantage of the fact that it allows him to do more things. Otherwise, just use the "weaker" (and thus much easier to understand) standard idiom of multiple catch blocks.
polygenelubricants
@polygenelubricants, point taken, thanks.
Yar