Java のネストされた Try ブロック
Java では、try ステートメントを別の try のブロック内に含めることができます。これはネストされた try ブロックと呼ばれます。try ステートメントが入力されるたびに、その例外のコンテキストがスタックにプッシュされます。
ネストされた try ブロックを使用する理由
場合によっては、ブロックの一部で 1 つのエラーが発生し、ブロック全体で別のエラーが発生するという状況が発生することがあります。このような場合、例外ハンドラーをネストする必要があります。
構文
try {
statement 1;
statement 2;
try {
statement 1;
statement 2;
}
catch(Exception e) {
// catch the corresponding exception
}
}
catch(Exception e) {
// catch the corresponding exception
}
.............
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
コマンド ラインなし:コマンド ライン引数なしでプログラムを実行すると、外側の try ブロックによってゼロ除算例外が生成されます。
1 つのコマンド ライン引数 : 1 つのコマンド ライン引数を使用してプログラムを実行すると、ネストされた try ブロック内からゼロ除算例外が生成されます。内側のブロックはこの例外をキャッチしないため、例外は外側の try ブロックに渡され、そこで処理されます。
2 つのコマンド ライン引数: 2 つのコマンド ライン引数を指定してプログラムを実行すると、内部の try ブロック内から配列境界例外が生成されます。それぞれのケースを説明するサンプル実行を次に示します。
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42
これは、特にリカバリ コードで例外がスローされる可能性がある場合には、避けられない場合があります。あまりきれいではありませんが、代替手段がない場合もあります。
サイドノート
- 内部の try ステートメントに、特定の例外に一致するcatch ステートメントがない場合、制御は、一致する catch ステートメントに必要な次の try ステートメントの catch ハンドラに転送されます。
- これは、catch ステートメントの 1 つが成功するか、ネストされた tryステートメントがすべて完了するまで続きます。
- どの catch ステートメントも一致しない場合は、Java ランタイム システムが例外を処理します。try ブロックに特定の例外のcatch ハンドラがないたびに、その catch ブロックが実行される一致が見つかった場合は、親 try ブロックの catch ブロックがその例外について検査されます。catch ブロックも親 catch ブロックも例外を処理しない場合そうすると、例外を処理しないときに表示されるものと同様に、ランタイム システムが例外に対して生成したメッセージが表示されます。
- ネストされた try ブロックが使用される場合、内側の try ブロックが最初に実行されます。内部の try ブロックでスローされた例外は、対応するcatch ブロックでキャッチされます。一致する catch ブロックが見つからない場合は、ネストされた try ステートメントがすべて使い果たされるまで、外側の try ブロックの catch ブロックが検査されます。一致するブロックが見つからない場合は、Java ランタイム環境が実行を処理します。
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
C:\>java NestTry
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One
a = 1
Divide by 0: java.lang.ArithmeticException: / by zero
C:\>java NestTry One Two
a = 2
Array index out-of-bounds:
java.lang.ArrayIndexOutOfBoundsException:42
/* Try statements can be implicitly nested via
calls to methods. */
class MethNestTry {
static void nesttry(int a) {
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out-of-bounds: " + e);
}
}
public static void main(String args[]) {
try {
int a = args.length;
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
System.out.println("a = " + a);
nesttry(a);
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
}
}
読んでいただきありがとうございます。楽しく学んでください
Mouad Oumous Java WhatsApp グループに参加する
Mouad Oumous Telegram チャンネルに参加する
フォローして私たちの出版物をサポートしてください

![とにかく、リンクリストとは何ですか?[パート1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































