例外がどのように伝播するかを理解するのに苦労しています。私のコード (以下の一般的なコードと非常によく似ています) が実行され、例外がスローされるotherCode()
と失敗します。一連のファイルを解析してループする必要があり、その一部は正しくフォーマットされていない可能性があり (例外が発生する)、解析されたデータをループすると、例外が発生する可能性があります (たとえば、ファイルのフォーマットは正しいがフィールドが欠落している)。 . これらの例外が発生すると、ループは残りのフィールド/ファイルで続行する必要があります。
私の混乱/不確実性のポイントは、以下のコメントの質問によって示されます (? によって)。continue outer
doSomething()
...
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?
}
}
}
また、入れ子になったdoSomething()
呼び出しによる伝播も理解していませnextMethod()
ん。たとえば、これらのメソッドに try-catch が含まれているか、処理がない場合...nextNextMethod()
doSomething()
throw new