java自定义异常怎么用_如何在Java中创建用户定义的异常(自定义异常)?

例外是程序执行期间发生的问题(运行时错误)。发生异常时,程序会突然终止,并且生成异常的行之后的代码将永远不会执行。

示例import java.util.Scanner;

public class ExceptionExample {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter first number: ");

int a = sc.nextInt();

System.out.println("Enter second number: ");

int b = sc.nextInt();

int c = a/b;

System.out.println("The result is: "+c);

}

}

输出结果Enter first number:

100

Enter second number:

0

Exception in thread "main" java.lang.ArithmeticException: / by zero

at ExceptionExample.main(ExceptionExample.java:10)

用户定义的例外

您可以使用Java创建自己的异常。所有例外必须是Throwable的子级。

如果要编写由Handle或Delare Rule自动执行的已检查异常,则需要扩展Exception类。

如果要编写运行时异常,则需要扩展RuntimeException类。

要创建用户定义的异常,请扩展上述类之一。要显示消息,请重写toString()方法,或者通过以String格式传递消息来调用超类参数化的构造函数。AgeDoesnotMatchException(String msg){

super(msg);

}

Or,

public String toString(){

return "CustomException[Age is not between 17 and 24]";

}

然后,在需要引发此异常的其他类中,创建创建的自定义异常类的对象,然后使用throw关键字引发该异常。MyException ex = new MyException ();

If(condition……….){

throw ex;

}

示例

在下面的Java程序中,我们将创建一个名为AgeDoesnotMatchException的自定义异常类。public class AgeDoesnotMatchException extends Exception{

(String msg){

super(msg);

}

}

另一个类Student包含两个私有变量名称age和一个初始化实例变量的参数化构造函数。

作为主要方法,我们接受用户的姓名和年龄值,并通过传递接受的值来初始化Student类。

在Student类的构造函数中,我们创建了一个异常AgeDoesnotMatchException的对象,并在年龄值介于17到24之间时引发了异常(使用throws)。public class Student extends RuntimeException {

private String name;

private int age;

public Student(String name, int age){

try {

if (age<17||age>24) {

String msg = "Age is not between 17 and 24";

AgeDoesnotMatchException ex = new AgeDoesnotMatchException(msg);

throw ex;

}

}

catch(AgeDoesnotMatchException e) {

e.printStackTrace();

}

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 Student: ");

String name = sc.next();

System.out.println("Enter the age of the Student should be 17 to 24

(including 17 and 24): ");

int age = sc.nextInt();

Student obj = new Student(name, age);

obj.display();

}

}

输出结果

在执行此程序时,您需要从键盘传递名称和年龄值。如果给定的年龄值id不在17到24之间,则发生异常,如下所示-Enter the name of the Student:

Krishna

Enter the age of the Student should be 17 to 24 (including 17 and 24):

14

AgeDoesnotMatchException: Age is not between 17 and 24

Name of the Student: Krishna'

Age of the Student: 14

at Student.(Student.java:18)

at Student.main(Student.java:39)

好的,让我来回答你的问题。 Java异常处理是一个非常重要的概念,它可以帮助我们更好地处理代码可能出现的错误情况,从而使我们的程序更加健壮。 在Java,有两种类型的异常:Checked Exception和Unchecked Exception。其,Checked Exception必须在代码显式地声明和处理,否则编译器会报错。而Unchecked Exception则不需要声明和处理,可以直接抛出。 当Java程序运行时,如果遇到了异常,它会自动抛出一个异常对象。我们可以通过try-catch语句来捕获并处理这些异常。如果我们想要抛出自己定义异常,可以通过自定义异常类来实现。 下面是一个Java自定义异常类的示例代码: ```java public class MyException extends Exception { public MyException(String message) { super(message); } } ``` 在上面的代码,我们定义了一个名为MyException的自定义异常类,它继承自Java的Exception类。通过继承Exception类,我们可以获得异常处理的一些基本功能,例如getMessage()方法来获取异常信息。 在MyException类,我们还定义了一个构造方法,它接受一个字符串参数message,并将其传递给父类的构造方法。 下面是一个Java抛出异常自定义异常类的应用示例代码: ```java public class Test { public static void main(String[] args) { try { int result = divide(10, 0); System.out.println(result); } catch (MyException e) { System.out.println(e.getMessage()); } } public static int divide(int x, int y) throws MyException { if (y == 0) { throw new MyException("除数不能为0"); } return x / y; } } ``` 在上面的代码,我们定义了一个名为Test的类,其包含了一个divide()方法和一个main()方法。 在divide()方法,我们判断了除数是否为0,如果是,就抛出一个MyException异常。 在main()方法,我们调用了divide()方法,并通过try-catch语句来捕获并处理可能抛出的MyException异常。 总之,Java异常处理是一个非常重要的概念,自定义异常类可以帮助我们更好地处理代码可能出现的错误情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值