欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();	
		}
	}
	
}