如何在java中创建一个“自定义异常类”?
1个回答
展开全部
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!
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!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询