如何在java中创建一个“自定义异常类”?

发布网友 发布时间:2022-04-22 22:11

我来回答

1个回答

热心网友 时间:2023-07-01 07:47

public class sssss {

public static void main(String[] args) {

System.out.print("Please input an Integer: ");

int value = new Scanner(System.in).nextInt();

try{
if(value < 0){
throw new MyException("My Exception comes!");
}
}catch (MyException myExp){
System.out.println("MyExcepton caugth!");
}

}
}

class MyException extends Exception{

public MyException(String s){
System.out.println("MyException occurs. The value must be greater than ZERO!");
}
}
-----------------------
Please input an Integer: -5
MyException occurs. The value must be greater than ZERO!
MyExcepton caugth!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com