Javaのカスタム例外
Javaで独自の例外を作成できます。これらは、ユーザー定義の例外またはカスタム例外と呼ばれます。
ユーザー定義の例外を作成するには、上記のクラスの1つを拡張します。メッセージを表示するには、 toString()を上書きします メソッドまたは、文字列形式のメッセージをバイパスしてスーパークラスのパラメーター化されたコンストラクターを呼び出します。
MyException(String msg){
super(msg);
}
Or,
public String toString(){
return " MyException [Message of your exception]";
} 次に、この例外を発生させる必要がある他のクラスで、作成されたカスタム例外クラスのオブジェクトを作成し、throwキーワードを使用して例外をスローします。
MyException ex = new MyException ();
If(condition……….){
throw ex;
} カスタムチェックとカスタムチェックなし
-
すべての例外はThrowableの子である必要があります。
-
ハンドルまたは宣言ルールによって自動的に適用されるチェック済み例外を作成する場合は、例外を拡張する必要があります。 クラス。
-
ランタイム例外を記述したい場合は、 RuntimeExceptionを拡張する必要があります クラス。
例:カスタムチェックされた例外
次のJavaプログラムは、カスタムチェック例外を作成する方法を示しています。
import java.util.Scanner;
class NotProperNameException extends Exception {
NotProperNameException(String msg){
super(msg);
}
}
public class CustomCheckedException {
private String name;
private int age;
public static boolean containsAlphabet(String name) {
for (int i = 0; i < name.length(); i++) {
char ch = name.charAt(i);
if (!(ch >= 'a' && ch <= 'z')) {
return false;
}
}
return true;
}
public CustomCheckedException(String name, int age){
if(!containsAlphabet(name)&&name!=null) {
String msg = "Improper name (Should contain only characters between a to z (all small))";
NotProperNameException exName = new NotProperNameException(msg);
throw exName;
}
this.name = name;
this.age = age;
}
public void display(){
System.out.println("Name of the Student: "+this.name );
System.out.println("Age of the Student: "+this.age );
}
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name of the person: ");
String name = sc.next();
System.out.println("Enter the age of the person: ");
int age = sc.nextInt();
CustomCheckedException obj = new CustomCheckedException(name, age);
obj.display();
}
} コンパイル時の例外
コンパイル時に、上記のプログラムは次の例外を生成します。
CustomCheckedException.java:24: error: unreported exception NotProperNameException; must be caught or declared to be thrown throw exName; ^ 1 error
例:カスタムのチェックされていない例外
カスタム例外が継承するクラスをRuntimeExceptionに変更するだけの場合 実行時にスローされます
class NotProperNameException extends RuntimeException {
NotProperNameException(String msg){
super(msg);
}
} 前のプログラムを実行する場合NotProperNameExceptionクラスを上記のコードに置き換えて実行すると、次のランタイム例外が生成されます。
実行時例外
Enter the name of the person: Krishna1234 Enter the age of the person: 20 Exception in thread "main" july_set3.NotProperNameException: Improper name (Should contain only characters between a to z (all small)) at july_set3.CustomCheckedException.<init>(CustomCheckedException.java:25) at july_set3.CustomCheckedException.main(CustomCheckedException.java:41)
-
Rubyのカスタム例外
Rubyで独自の例外を作成するのは簡単です。次の手順に従ってください: 1。新しいクラスを作成する 例外は、Rubyの他のすべてと同じように、クラスです。新しい種類の例外を作成するには、StandardErrorまたはその子の1つから継承するクラスを作成するだけです。 class MyError < StandardError end raise MyError 慣例により、新しい例外のクラス名は「エラー」で終わります。カスタム例外をモジュール内に配置することもお勧めします。つまり、最終的なエラークラスは次のようになります:ActiveRecord::RecordNotFound
-
Rubyで例外を救出する
発生した例外をレスキューして、アプリケーションが呼び出しスタックの最上位に到達したときにアプリケーションがクラッシュするのを防ぐことができます。 Rubyでは、rescueを使用します そのためのキーワード。 Rubyで例外をレスキューするときに、レスキューする必要のある特定のエラークラスを指定できます。 begin raise 'This exception will be rescued!' rescue StandardError => e puts "Rescued: #{e.inspect}" end 注 :raiseを使用す