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