java自定义异常类的使用
程序员文章站
2022-03-15 11:43:11
...
package kaoshi;
import java.util.Scanner;
/**
************************************
* @author Hejing
* @date 2017年12月24日
* @class testshu.java
*
************************************
*/
class MyException extends Exception{
public void chucuo() {
System.out.println("答案出错");
}
}
public class testshu {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in );
try {
System.out.println("2+3=?,请输入结果:");
if(5!=sc.nextInt()) {
throw new MyException();
}
else {
System.out.println("回答正确");
}
}
catch(MyException e1 ) {
e1.chucuo();
}
}
}
上一篇: Java自定义异常类的简单实现
下一篇: 自定义异常类的简单运用