例外クラス



Exception Class



まず、例外は何ですか:

例外は、実行時に発生する問題です。通常、例外として説明されます。



Javaでは、例外はクラスにカプセル化されます。問題が発生すると、例外クラスオブジェクトが作成され、例外関連の情報(詳細、名前、例外の場所など)がスローされます。

第二に、異常な継承関係:



Throwableクラスは、すべてのエラークラスと例外クラスのスーパークラス(祖先クラス)です。

例外例外クラスとそのサブクラスはThrowableクラスから継承され、Javaで発生する可能性のある例外を表すために使用され、これらの例外を合理的に処理します。

RuntimeExceptionクラスは、Exceptionクラスから継承する実行例外クラスです。それとそのサブクラスは、実行中にのみ存在できます。それらが表示された場合、変更できるのはソースコードのみです。この例外は処理できません。



Errorクラスは、Exceptionと並行するクラスであり、Javaで重大なエラーを表すために使用されます。コードを変更することによってのみ解決できます。

継承システムの概要:

Throwable: It is the superclass of all errors and exceptions (the ancestor class) |- Error error |- Exception compile-time exception, problems when compiling JAVA programs |- RuntimeException runtime exception, problems in the JAVA program running

第三に、例外とエラーの違い:

例外は、プログラムのコンパイルまたは実行時に発生する例外です。例外を除いて何かをすることができます。例外が処理されない場合、プログラムは実行を停止します。

このエラーは、プログラムの実行中に発生する重大な問題を示しています。処理できず、プログラムの実行が停止します。エラーは通常、システムレベルの問題です。これは、仮想マシンのjvmシステムが原因で発生します。ソースコードを変更することによってのみ問題を解決することができます。

第四に、異常な生産のプロセス:

1.実行またはコンパイル時に例外を生成します

2.例外クラスのオブジェクトを作成します

3.例外クラスを宣言します

4.例外クラスオブジェクトを呼び出し元に渡します(main()メソッド)

5.呼び出し元は処理できず、例外クラスオブジェクトをjvm仮想マシンに渡します。

6.jvm仮想マシンは、例外クラスの情報(名前、詳細情報、例外の場所)を画面に出力し、プログラムの実行を停止します。

5、例外スローをスローします

Javaでは、指定された例外をスローするためにthrowキーワードが提供されています。

指示:

1.例外オブジェクトを作成します。いくつかのヒントをカプセル化する

2.この例外について発信者に伝えます

使用形式:

新しい例外クラス名(パラメーター)をスローします

第六に、異常な投球を宣言する

例外フォーマットを宣言します:

修飾子の戻り値タイプメソッド名(パラメーター)は、例外1名、例外2名をスローします{

}

セブン、異常なトライをキャプチャ...キャッチ...最後に

例外のキャプチャ:Javaは例外固有のステートメントをキャッチし、指定された方法で発生する例外を処理できます。

ステートメント形式:
コードをコピーする

{を試してください

//The statement that needs to be detected.

}

キャッチ(例外クラス変数){//パラメーター。

//Exception handling statement.

}

最後に {

//The statement that will be executed.

}

試してください:例外を生成する可能性のあるコードブロックにコードを記述してください。

キャッチ:特定の例外をキャプチャし、キャプチャされた例外を処理するために使用されます。

最後に:例外が発生するかどうかに関係なく、実行する必要のある特定のコードがいくつかあります。また、例外によりプログラムジャンプが発生するため、一部のステートメントは実行されません。最後に、この問題を解決するために、finallyコードブロックに格納されているコードが実行されます。

例外をキャプチャする方法:

  1. キャッチファイナルの組み合わせを試してください:例外を検出してキャッチハンドラーに渡し、リソースをファイナルで解放します。

  2. キャッチの組み合わせを試してください:コードの異常検出、および検出された例外のキャッチ処理への受け渡し。例外をキャプチャします。

  3. 複数のcatchの組み合わせを試してください:コードの異常検出、および検出された例外のcatch処理への受け渡し。例外情報ごとに異なるキャプチャ処理が実行されます。

注:この例外処理方法では、複数のキャッチの例外を同じにすることはできません。キャッチ内の複数の例外の間にサブ親の例外がある場合、サブクラスの例外は上記のキャッチで処理する必要があり、親は異常な。次のキャッチ処理で。

  1. 最後に組み合わせてみてください。コードの例外検出。例外を検出した後、キャッチがないため、デフォルトのjvmによってスローされます。例外はキャプチャによって処理されません。ただし、関数によって開かれたリソースは、最終的にすべて閉じる必要があります。リソースを閉じるだけです。

8つの実行時例外RuntimeExecption

特徴:

メソッドで実行時例外がスローされ、メソッド定義でthrows宣言は不要であり、呼び出し元はこの例外を処理する必要はありません。

ランタイム例外が発生すると、プログラム担当者はソースコードを変更する必要があります。

9、メソッドの例外は詳細を書き換えます

  1. サブクラスがスーパークラスメソッドをオーバーライドするときに、親クラスのメソッド宣言が異常である場合、サブクラスは親クラスの例外または例外のサブクラスのみを宣言できるか、宣言できません。
class Fu { public void method () throws RuntimeException { } } class Zi extends Fu { Public void method() throws RuntimeException { } // throws the same exception as the parent class //public void method() throws NullPointerException{ } //Throw the parent class exception }

2.親クラスのメソッドが複数の例外を宣言する場合、子クラスが上書きされると、複数の例外のサブセットのみを宣言できます。
コードをコピーする

class Fu { public void method () throws NullPointerException, ClassCastException{ } } class Zi extends Fu { public void method()throws NullPointerException, ClassCastException { } Public void method() throws NullPointerException{ } // throws a part of the parent class exception Public void method() throws ClassCastException { } // throws a part of the parent class exception }

3.オーバーライドされるメソッドに例外宣言がない場合、サブクラスが上書きされるときに例外を宣言することはできません。

class Fu { public void method (){ } } class Zi extends Fu { Public void method() throws Exception { }//Wrong way }

X.異常の一般的な方法

Throwableクラスでは、例外オブジェクトを操作するための多くのメソッドを提供します。一般的に使用される方法は次のとおりです。

1. getMessageメソッド:例外の詳細情報文字列、つまり例外プロンプト情報を返します

  1. toStringメソッド:例外の名前と詳細文字列を返します

  2. printStackTrace:例外の名前と詳細文字列をコンソールに出力します。これは、例外が発生するコードの場所です。

11、カスタムクラスの例外

フォーマット:

Class exception name extends Exception { // or inherits RuntimeException Public exception name(){ } Public exception name (String s){ super(s) } }

カスタム例外継承例外デモ:

class MyException extends Exception{ /* Why define a constructor because you see an exception in the Java description class that provides an initialization method for the exception object. */ public MyException(){ super() } public MyException(String message) { Super(message)// If the custom exception requires exception information, you can call the constructor of the parent class with a string argument. } }

カスタム例外はRuntimeExceptionデモを継承します。

class MyException extends RuntimeException{ /* Why define a constructor because you see an exception in the Java description class that provides an initialization method for the exception object. */ MyException(){ super() } MyException(String message) { Super(message)// If the custom exception requires exception information, you can call the constructor of the parent class with a string argument. } }

サンプル

package com.oracle.Demo01 public class Demo01 { //try/catch format: used to handle exceptions // try{ // code being detected (code that may have an exception) // }catch (exception class name variable) { // exception handling // }finally{ // code that must be executed // } public static void main(String[] args) { int[] arr={1,2,3} try{ int res=getArray(arr) System.out.println(res) }catch(ArrayIndexOutOfBoundsException ex1){ //Multi-catch processing, there is no order of the level System.out.println(ex1) //When the parent-child relationship appears, be sure to put the sub-class in front. System.out.println('===========') System.out.println(ex1.getMessage()) System.out.println('===========') System.out.println(ex1.toString()) }catch(NullPointerException ex2){ System.out.println(ex2) System.out.println(ex2.getMessage()) System.out.println('===========') ex2.toString() }finally{ System.out.println('code that must be executed') } System.out.println('I executed?') } //The difference between throw and throws is used to report exceptions. After the //throw keyword, it must be followed by an exception object. // The throws keyword must be followed by the exception class name to pass the exception to the caller. public static int getArray(int[] arr) throws ArrayIndexOutOfBoundsException,NullPointerException{ if(arr==null){ Throw new NullPointerException('Array is empty o(╯□╰)o') } if(arr.length<4){ Throw new ArrayIndexOutOfBoundsException('The array length is not enough (キ`゚Д゚ ́)!!') } int i=arr[3]*2 System.out.println('aaa') return i } }