いくつかの例外をキャッチする方法?
1848 ワード
この記事では、いくつかの例外をキャッチする方法と、正しい順序でそれらを記述する方法を見ていきます.
いくつかの例外をキャッチ
いくつかの例外に対処する方法を理解する例を挙げましょう.
出力
番号をゼロに分ける必要はありません
トライキャッチブロックから
上の例では、tryブロックの算術演算例外に対する最初のcatchブロックを実行します.
では、コードを少し変更して出力の変更を見ましょう.
いくつかの例外をキャッチ
いくつかの例外に対処する方法を理解する例を挙げましょう.
class Example {
public static void main (String args []) {
try{
int arr [] = new int [7];
arr [4] = 30/0;
System.out.println ( "Last Statement try block");
}
catch (ArithmeticException e) {
System.out.println ( "You do not have to divide a number by zero");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ( "Accessing array elements beyond the bounds");
}
catch (Exception e) {
System.out.println ( "Some other Exception");
}
System.out.println ( "Out of the try-catch block");
}
}
出力
番号をゼロに分ける必要はありません
トライキャッチブロックから
上の例では、tryブロックの算術演算例外に対する最初のcatchブロックを実行します.
では、コードを少し変更して出力の変更を見ましょう.
class Example {
public static void main (String args []) {
try{
int arr [] = new int [7];
arr [10] = 10/5;
System.out.println ( "Last Statement try block");
}
catch (ArithmeticException e) {
System.out.println ( "You do not have to divide a number by zero");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ( "Accessing array elements beyond the bounds");
}
catch (Exception e) {
System.out.println ( "Some other Exception");
}
System.out.println ( "Out of the try-catch block");
}
}
Read moreReference
この問題について(いくつかの例外をキャッチする方法?), 我々は、より多くの情報をここで見つけました https://dev.to/joshuabrown0010/how-to-catch-a-few-exceptions-4128テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol