测试是我职业生涯中最喜欢的工作,也希望自己在这个空间里每天进步一点点,改善自己,提高自己。

Java学习-lesson4(下)-异常

上一篇 / 下一篇  2008-02-26 17:01:35 / 个人分类:Java-lesson4(下)

1.异常定义了程序中遇到的致命的错误,而不是编译时的语句错误,
当程序发生异常,则程序中就会生成一个异常的对象传递给catch
2.try,catch语句
 try{
   new Test().devide(3.0);
   }
   catch(Exceptong e)//e 是异常的对象
   {
   System.out println(e.getMessage())
   }
当程序发生异常,则程序中就会生成一个异常的对象传递给catch
3.throws关键字
class Test
{
     public int devide(int x,int y) throws exception
  {
  int result =x/y;
  return result
  }
  
}

class TestException()
{
     public static void main(String[] arg)
  {
      try{
   new Test().devide(3.0);
   }
   catch(Exceptong e)
   {
   System.out println(e.getMessage())
   }
   system.out println("program is running here!")
  }
}
如果类Test有throws 则TestException 中必须有try--catch语句
或者:

class Test
{
     public int devide(int x,int y) throws exception
  {
  int result =x/y;
  return result
  }
  
}

class TestException()
{
     public static void main(String[] arg) throws Exception
  {
   new Test().devide(3.0);
   system.out println("program is running here!")
  }
}
但是实际编程中不要用这种方式要用 try catch语句

4.自定义异常与Throw关键字
自定义的异常类要继承Exception类
class Test
{
     public int devide(int x,int y) throws exception
  {
   if(y<0)
   throw new DevideByMinusException("Devisor is"+ y)//throw 抛出异常
  int result =x/y;
  return result
  }
  
}
class DevideByMinusException extends Exception
{
     public DevideByMinusException(String msg)
     {
    super(msg);
  }
}//自定义的类

class TestException()
{
     public static void main(String[] arg)
  {
      try{
   new Test().devide(3.-1);
   }
   catch(Exceptong e)
   {
   System.out println(e.getMessage())
   }
   system.out println("program is running here!")
  }

5.多个异常作处理
public int devide(int x,int y) throws ArithmeticException,DevideByMinusException
  //抛出多个异常,ArithmeticException,是算数异常

 try{
   new Test().devide(3.-1);
   }
   catch(Exception e)
   {
   }
   catch(ArithmeticException ex)
   {
       System.out println(("Devisor is running));
      ex.printStackTrace();//直接把异常打印到屏幕上
   }
   catch(DevideByMinusException e)
   {
       System.out println(("Devisor is"+ y));
     ex.printStackTrace();
   }
  注意:catch(Exception e)
   {
   }

不能放在其他的catch语句的前面,必须放在最后
6.可以在一个方法中使用 try....catch语句throws语句来实现程序的跳转
用throw 抛出对象,用try....catch语句捕获,可以实现程序的跳转
   void fun()
   {
     try
  {
     if (x==0)
        throw new XxxException("xxx");
     else
        throw new YyyException("yyy");
  }
  catch(XxxException("xxx"));
  {
 
  }
   catch(YyyException("yy"));
  {
 
  }
   }
7.一个父类的方法被覆盖时,子类覆盖父类的方法必须扔出和父类相同的异常或者是抛出异常的子类。可以不抛出异常
8.如果父类扔出多个异常,那么重写(覆盖)方法必须扔出那些异常的一个子集,也就是说不能扔出新的异常
 
9.Exception 类的父类是Trowable
10.finally
try{
   new Test().devide(3.-1);
   }
  
   catch(ArithmeticException ex)
   {
       System.out println(("Devisor is running));
       ex.printStackTrace();//直接把异常打印到屏幕上
   }
   catch(DevideByMinusException e)
   {
       System.out println(("Devisor is"+ y));
     ex.printStackTrace();
   }
    catch(Exception e)
   {
   }
   finally//不管try中的代码是否有异常,finally中的代码都要执行
          //唯一不被执行的情况就是:在catch语句中增加了System.exit(0)[表示程序到此结束]语句

   {
        System.out println("finally/");
   }


TAG:

 

评分:0

我来说两句

日历

« 2024-05-05  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 15698
  • 日志数: 33
  • 图片数: 1
  • 建立时间: 2008-02-18
  • 更新时间: 2008-09-18

RSS订阅

Open Toolbar