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

在一个方法中抛出自定义异常,如何在该方法的catch处理程序中捕获并重新抛出

第1个回答  推荐于2019-10-19
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!本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网