2

I need to know if doing less Try and doing more Catchs is a good way to control a flux what is very important in exception control terms.

Because in the flux this should never go on if there's something goes wrong!

i'm not trying to save code lines, i need something visually easy to understand and functionally in code terms

Var ix; Var iy; Var iz; try { try { do something ix; } catch (Exception ex1) { ex1.printStackTrace(); } try { do something iy; } catch (Exception ex2) { ex2.printStackTrace(); } do something iz; } catch (Exception ex3) { ex3.printStackTrace(); } 

OR

Var ix; Var iy; Var iz; try { do something ix; do something iy; do something iz; } catch (Exception ex1) { ex1.printStackTrace(); } catch (Exception ex2) { ex2.printStackTrace(); } catch (Exception ex3) { ex3.printStackTrace(); } 
4
  • 3
    The second code is invalid, you can't catch exceptions of type "Exception" in more that one block. Commented Oct 17, 2013 at 14:29
  • 3
    The first thing to consider is whether you should actually be catching all those exceptions to start with. Are you actually handling them? Just printing a stack trace and continuing as if nothing had gone wrong is almost always the wrong approach. Commented Oct 17, 2013 at 14:29
  • @DanielR Most probably those are just for example, to explain the doubt. Commented Oct 17, 2013 at 14:32
  • 2
    Catching exceptions without properly handling them is the most common stupid mistake in Java. In 90% of all cases, letting it bubble up is the right thing. In 90% of the remaining cases, wrapping it is the right thing. Commented Oct 17, 2013 at 14:51

6 Answers 6

10

Those two examples will actually behave differently. In the second, if an exception is caught then none of the following do something will run. In the first they can each fail independently.

Sign up to request clarification or add additional context in comments.

Comments

7

Always catch exception in as small and limited scope as possible; using too broad catches like

} catch (Exception ex) { // wrong. use explicit exceptions, // e.g. catch(NullPointerException ex) instead, possibly with multicatch 

or catches on too big scope

try { //... tons of code } catch (IOException ex) { // and the exception happened WHERE? } 

is an anti-pattern by itself.

This thing said, all that other people said is correct. The codes will behave differently, because nested catches actually handle the exception encountered within and not propagate it outside.

Multiple catches on e.g. Exception (replace with e.g. NullPointerException and the idea is still there) done in this fashion

try { try { // some code } catch (Exception ex1) { /* stack dump or whatever */ } } catch (Exception ex2) { /* stack dump or whatever */ } 

won't work good too, because inner Exception is a valid catch for every exception of a given kind (in this case - every exception at all) - thus nothing catchable will propagate outside of it to the outer try/catch block.

Another thing (already stated countless times too) is that you should either cure the exception, throw it up (propagate) or die with the exception. Simply doing a stack dump is like saying 'you, my dear sir, have cancer'. It doesn't improve the situation for anybody. If you can't help it, propagate it. If nothing can handle it, die with some grace (stack dump is hardly a gracious way for a thread to die...).

And yet another thing to distinguish is Java's Throwable types, i.e. error vs checked/unchecked exception difference. In general:

  • Error should cause the thread to die immediately, because it signifies a serious VM/hardware error,
  • unchecked exception (RuntimeException comes to mind) is a really exceptional case, impossible or very hard to predict, so it should usually NOT be contained and rather just signaled to the user with programatic fallback to last validly executed code branch,
  • checked exception is a common exceptional case, which, in turn, MUST be handled by the programmer explicitly and shouldn't disrupt program execution.

tl;dr - please learn the rationale about using exceptions before actually using them.

Further reading: http://docs.oracle.com/javase/tutorial/essential/exceptions/ and http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp/0321356683 (Item 39: Use exceptions only for exceptional conditions, Item 40: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors, Item 41: Avoid unnecessary use of checked exceptions, Item 43: Throw exceptions appropriate to the abstraction. )

1 Comment

Dear Downvoter - if you'd use a comment feature instead of downvote, you'd provide valuable input into the collectible knowledge of SO users. If you'd downvoted and commented on the reason, you'd increase my knowledge. Since you haven't provided any reason for downvote, I can only assume you're either clueless or just want to irritate other people. BTW, I am aware that this question answers more than OP asked - the reason for this is simple, OP clearly hadn't the understanding of the subject necessary to properly solve the problem at hand - which I tried to provide.
4

The two ways are logically different. In the first way, you will try to do action "iy" even if action "ix" throws an exception. In the second way, if action "ix" throws an exception, then "iy" and "iz" will never be executed.

Comments

4

I would say it depends on what you want. Are any of your exceptions recoverable? Do you want to keep going after encountering an exception? These kind of things determine how you handle exceptions. Or at least, how I do.

Comments

4

This is very different !

In your first code :

  • If ix triggers ex1, it will continue to iy. But if it triggers ex3, everything will stop.
  • If iy triggers ex2, it will continue to iz. But if it triggers ex3, everything will stop.

In your second code :

  • if an exception is thrown, everything will stop.

You should read this tutorial.

1 Comment

In 1st example, you won't be able to trigger ex3 with ix nor iy btw... you could just place the catch(ex3) on the iz block, improving the code both logically and visually. OR differentiate the Exceptions to SomeException, OtherException, AnotherException etc... remember, that every exception has to extend Exception, and is thus catchable by catch(Exception ex)
2

There is no single, correct answer to your question. It depends. As you can see from other answers your first and second code differs how in execution (some operations might be skipped in second example). You should focus on:

  • type of exceptions you are catching (are those exceptions different, do you want to handle them differently?),
  • how you are going to handle the exception,
  • is one exception affecting other pieces of your code,
  • are two or more exceptions going to be catch by single catch statement,
  • etc.

You should consider each solution for given circumstances.

1 Comment

this was the answer!!! yeah i know the Exception what i need to handle and the "general" exception... in control of the flux i need what any error can make a general stop!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.