《Thinking in Java》上对这章的讲解不少,可见重要性,学习和总结一些主要的记录下来。

一、创建自定义异常


 1 package Exception;
 2 
 3 class SimpleException extends Exception{}
 4 
 5 public class InheritingException{
 6     
 7     public void f() throws SimpleException {
 8         System.out.println("Throw SimpleException from f()");
 9         throw new SimpleException();
10     }
11     
12     public static void main(String[] args) {
13         InheritingException sed = new InheritingException();
14         try {
15             sed.f();
16         } catch (SimpleException e) {
17             e.printStackTrace();
18         }
19     }
20     
21 }
1 输出:
2 Throw SimpleException from f()
3 Exception.SimpleException
4     at Exception.InheritingException.f(InheritingException.java:10)
5     at Exception.InheritingException.main(InheritingException.java:19)

  throw与throws的区别与详情

  编译器创建了默认构造器,它将自动调用基类的默认构造器。

  对异常来说,最重要的部分就是类名,其它也没用,可以增加一个带参的构造方法。

  比如NullPointerException:

 1 public
 2 class NullPointerException extends RuntimeException {
 3     private static final long serialVersionUID = 5162710183389028792L;
 4 
 5     /**
 6      * Constructs a {@code NullPointerException} with no detail message.
 7      */
 8     public NullPointerException() {
 9         super();
10     }
11 
12     /**
13      * Constructs a {@code NullPointerException} with the specified
14      * detail message.
15      *
16      * @param   s   the detail message.
17      */
18     public NullPointerException(String s) {
19         super(s);
20     }
21 }

 

二、捕获异常


  1)try块

    如果在方法内部抛出了异常(或者在方法内部调用的其他方法抛出了异常),这个方法将在抛出异常的过程中结束。

    要是不希望方法就此结束,可以在方法内设置一个特殊的块来捕获异常。

try{
  //exceptions  
}

   2)异常处理程序

    异常处理程序紧跟在try块之后,以关键字catch表示:

try{
  //exceptions    
} catch(Type1 id1) {
  //Type1  
} catch(Type2 id2) {
  //Type2
}

    当异常被抛出时,异常处理机制将负责搜寻参数与异常类型相匹配的第一个处理程序。然后进入catch子句执行,此时认为异常得到了处理。

    注意,只有匹配的catch子句才能得到执行,这与switch语句不同。

   3)栈轨迹

    printStackTrace()方法所提供的信息可以通过getStackTrace()方法来直接访问,这个方法将返回一个由栈轨迹中的元素所构成的数组,其中每一个元素都表示

  栈中的一帧。元素0是栈顶元素,并且是调用序列中的最后一个方法调用。数组中最后一个元素和栈底是调用序列中的第一个方法调用。

 1 public class WhoCalled {
 2     static void f() {
 3         try {
 4             throw new Exception();
 5         } catch (Exception e) {
 6             for(StackTraceElement ste : e.getStackTrace()) {
 7                 System.out.println("line: " + ste.getLineNumber() + " method: " + ste.getMethodName());
 8             }
 9         }
10     }
11     static void g() {f();}
12     static void h() {g();}
13     public static void main(String[] args) {f();g();h();}
14 }

  程序输出:

line: 5 method: f
line: 14 method: main
line: 5 method: f
line: 12 method: g
line: 14 method: main
line: 5 method: f
line: 12 method: g
line: 13 method: h
line: 14 method: main
View Code

相关文章:

  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2022-02-08
  • 2021-10-31
  • 2021-06-05
  • 2021-07-14
  • 2021-10-27
猜你喜欢
  • 2021-12-23
  • 2021-08-26
  • 2021-07-20
  • 2021-10-02
  • 2022-12-23
  • 2021-12-08
  • 2021-10-13
相关资源
相似解决方案