java自定义异常类的使用

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

 

猜你喜欢

转载自blog.csdn.net/qq_37843372/article/details/80170503