java中异常的注意?

来源:互联网 发布:淘宝闲置店铺怎么查找 编辑:程序博客网 时间:2024/05/19 20:43

public class ExceptionTest {

 public static void main(String[] args) {
  try {

   Integer.parseInt(args[0]);

   try {

    throw new MyException("My Exception");// 1.抛出异常

   } catch (MyException e) {// 捕获MyException

    System.out.println(e.getMessage());// 2.先捕获到异常

    throw e;// 3.再抛出异常

   }

  } catch (Exception e) {

   System.out.println(e.getMessage());

  }

 }
}

class MyException extends Exception {
 public MyException (String arg){
  super(arg);
 }
}

执行结果:

My Exception
My Exception

总结:抛出异常后,最里层的catch先捕获到异常。

原创粉丝点击