您的位置:首页 > 编程语言 > Java开发

java 基础(异常练习)

2015-12-07 14:18 603 查看
class LpException extends Exception
{
	LpException(String message)
	{
		super(message);
	}
}

class SmokeException extends Exception
{
	SmokeException(String message)
	{
		super(message);
	}
}

class NoteException extends Exception
{
	NoteException(String message){
		super(message);
	}
}

class Computer
{ 
	private int state=3; 
	void run()throws LpException,SmokeException
	{	
		System.out.println("start");
		if(state==2)
			throw new LpException("langpin");
		if(state==3)
			throw new SmokeException("smoke");	
	}
	void close()
	{ 
		state=1;
		System.out.println("stop");
	}
}

class Teacher 
{
	private String name;
	Computer pc;
	Teacher(String name)
	{
		this.name =name;
		pc=new Computer();
		
	}
	void teaching()throws NoteException
	{
		try
		{
			pc.run();
		}
		catch(LpException e)
		{
			pc.close();
		}
		catch(SmokeException e)
		{
			test();
			throw new NoteException(e.getMessage()+"  break");
		}
		System.out.println("teaching");
	}
	void test()
	{
		System.out.println("testing");
	}
	
}

class MI
{
	public static void main(String[] args)
	{
		Teacher t=new Teacher("bi");
		try
		{
			t.teaching();
		}
		catch(NoteException e)
		{
			System.out.println(e.toString());
			System.out.println("change");
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: