天天看点

Java学习笔记——异常中的继承问题

<span style="font-size:18px;">
</span>
           
<span style="font-size:18px;">class AException extends Exception
{
}

class BException extends AException
{
}

class Fu
{
	public void show() throws AException
	{
	
	}
}

class Zi extends Fu
{
	public void show() throws BException //或者抛出 BException
	{
	
	}
}

class ExceptionDemo3

{
	public static void main(String[] args) 
	{
		try{
		Fu f = new Fu();
		f.show();
		}catch(AException e)//只能抛出AException
		{
		
		}
		try{
		Zi z = new Zi();
		z.show();
		}catch(AException e) //当子类中方法抛出BException时这也要抛出BException
		{
		
		}

		try{
		Fu f = new Zi();
		f.show();
		}catch(AException e)//只能抛出AException,因为多态
		{
		
		}
	}
}</span>