views:

39

answers:

1

I'm having trouble understanding how exceptions should propagate. My code (quite similar to the generic code below) executes otherCode() and fails to continue outer when doSomething() throws the exception. I need to loop over parsing a bunch of files, some of which may be formated incorrectly (causing an exception), and then loop over the parsed data, which can also lead to an exception (e.g. file had correct format but a missing field). When these exceptions occur, the loops ought to continue on the rest of the fields/files.
My points of confusion/uncertainty are indicated by questions in the comments below (by the ?'s).

...
public static void main(string[] args) {
   outer: while ( thingsToDo ) {
       try{
           someItrType someIterable = doSomething(); // might throw
           otherCode();  // only do this if nothing was thrown above?
       } catch (SomeExceptionType e) {
           handleSomeExceptionType();
           continue outer; // keep trying the rest of the loop?
       }
       otherOtherCode(): // only if nothing thrown, because of the continue?
       inner: while( someIterable.next() ) {
            try{
                doSomethingElse();  // might throw
            } catch (SomeExceptionType e) {
                handleSomeExceptionType();
                continue inner; // keep trying the inner loop?
            }
            doThisIfAllOkay();  // only if no exceptions thrown?
        }
    }
}

I also don't understand propagation through nested calls, e.g. if doSomething() calls nextMethod(), which in turn calls nextNextMethod() and either of those throw exceptions, when will execution catch in those methods vs. in the try-catch block around doSomething()? For example, if those methods throw new, contain a try-catch or have no handling...

+3  A: 

When an exception occurs in a try block, it immediately causes program flow to move to the catch block. Any code after the exception occurred in the try block will not execute.

Using the continue keyword causes the loop to skip to the next iteration, starting from the top of the loop, so any code below the catch block won't execute either.

As far as nested functions go:
There are 2 options for exceptions handling. Either you deal with it yourself, or you declare that your function can throw an exception (of that particular type). Assuming that every single function in your program declares that it throws Exception, then the exception would bubble up out of all your code all the way to the Java Virtual Machine, which would then terminate. Basically, nested functions with an exception will keep passing it up the stack until it is handled in a try/catch block.

Sam Dufel
And the try-catch will stop propagation unless the exception is explicitly rethrown in the catch block?
reve_etrange
...or if no appropriate catch block is encountered the program terminates. (for completeness on an otherwise stellar answer, +1)
Mark E
@Mark - You are too kind, sir @reve - yes, try-catch stops propogation, as long as try-catch specfies the correct Exception type
Sam Dufel
Thanks for the very clear explanation. Now I just need to figure out what to do when this try-catch is deep inside a bunch of method calls in someone else's library...
reve_etrange