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

发布新日志

  • Java学习-lesson4(下)-包

    2008-02-29 16:45:00

    43分钟
    1.package语句及应用
    2.package语句作为java源文件的第一条语句,如果没有package语句,则缺省默认无名包
    package test.lesson
    运行java时要
    java 包名.java名

    要提前把包的文件夹创建好目录结构 test\lesson

    也可以不提前创建好,只要在运行javac时加上-d . 就可以自动创建目录结构
    例如 javac -d . lesson33.java就可以
    -d编译的结果放在目录下,.表示当前目录。

  • Java学习-lesson4(下)-异常

    2008-02-26 17:01:35

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

Open Toolbar