开发者

Exception propagation through nested loops/calls

开发者 https://www.devze.com 2023-01-22 07:13 出处:网络
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 excep

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

...开发者_StackOverflow中文版
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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消