异常

来源:互联网 发布:linux rar压缩命令 编辑:程序博客网 时间:2024/04/29 23:19

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

原创粉丝点击