java例程练习(自定义异常处理)

public class Test {
	public void regist(int num) throws MyException {
		if(num < 0)	{
			throw new MyException("人数为负值,不合理", 3);
		}
		System.out.println("登记人数" + num);
	}
	
	public void manager(int k) {
		try {
			regist(k);
		} catch(MyException me) {
			System.out.println("登记出错,出错类型:" + me.getId());
			me.printStackTrace();
		}
		System.out.println("操作结束");
	}
	
	public static void main(String[] args) {
		Test t = new Test();
		t.manager(-1);
		t.manager(100);
	}
	
}

class MyException extends Exception {
	private int id;
	public MyException(String message, int id) {
		super(message);
		this.id = id;
	}
	
	public int getId() {
		return id;
	}
}

posted on 2012-04-22 22:11  Yours风之恋  阅读(138)  评论(0编辑  收藏  举报