class Test
{
public int devide(int x,int y) throws ArithmeticException,DevideByMinueException
{
if(y<0)
throw new DevideByMinueException("除数为"+y);
int result = x/y;
return result;
}
}
class DevideByMinueException extends Exception
{
public DevideByMinueException(String msg)
{
super(msg);
}
}
class SubTest extends Test
{
public int devide(int x,int y) throws ArithmeticException//此处只能抛出父类中存在的异常
{
if(y<0)
throw new ArithmeticException("除数为"+y);
int result = x/y;
return result;
}
}
class TestException
{
public static void main(String args[])
{
try
{
new Test().devide(3,0);
}catch(ArithmeticException ex)
{
System.out.println(" ArithmeticException");
ex.printStackTrace();
}catch(DevideByMinueException ex)
{
System.out.println(" DevideByMinueException");
ex.printStackTrace();
}
catch(Exception ex)//Exception 语句不能放在其他异常语句之前
{
ex.printStackTrace();
System.exit(0);
}
finally
{
System.out.println("finally");
}
}
}
异常
最新推荐文章于 2024-02-01 00:49:42 发布