Thinking in java4 Exception

来源:互联网 发布:举报网络电子邮箱地址 编辑:程序博客网 时间:2024/06/11 18:08

Thinking in java4 Exception

一  创建自定义异常 异常和日志
LoggingExceptions:
LoggingExceptions2:
打印日志

StringWriter trace = new StringWriter();printStackTrace(new PrintWriter(trace));logger.severe(trace.toString());ExtraFeatures: e.printStackTrace(System.out) ,e.printStackTrace(System.err) ==e.printStackTrace()

一  捕获异常,异常打印源码
ExceptionMethods:
e.getMessage() ==e.getLocalizedMessage()
e = e.getClass().getName() + e.getLocalizedMessage()
e.printStackTrace(System.out) printStackTrace 获取堆栈信息 StackTraceElement。

WhoCalled:
记录堆栈的类,方法,行,可以看看StackTraceElement.toString()就是我们的异常信息,e.getStackTrace()获取堆栈信息

Rethrowing:
去除之前的异常堆栈,从当前点打印异常堆栈, throw (Exception) e.fillInStackTrace();

一  不用抛出异常RuntimeException
NeverCaught:不用抛出异常

//: exceptions/NeverCaught.java// Ignoring RuntimeExceptions.// {ThrowsException}public class NeverCaught {  static void f() {    throw new RuntimeException("From f()");  }  static void g() {    f();  }  public static void main(String[] args) {    g();  }} ///:~

一  异常丢失
LostMessage:finally抛出异常,导致之前的异常丢失。
ExceptionSilencer

//: exceptions/ExceptionSilencer.javapublic class ExceptionSilencer {  public static void main(String[] args) {    try {      throw new RuntimeException();    } finally {      // Using 'return' inside the finally block      // will silence any thrown exception.      return;    }  }} ///:~

一 异常重载
StormyInning : 抛出子类最好,先处理子类异常,在处理基类异常。

一 构造函数抛出异常,try 嵌套
Cleanup: 两个try捕获
CleanupIdiom

//: exceptions/CleanupIdiom.java// Each disposable object must be followed by a try-finallyclass NeedsCleanup { // Construction can't fail  private static long counter = 1;  private final long id = counter++;  public void dispose() {    System.out.println("NeedsCleanup " + id + " disposed");  }}class ConstructionException extends Exception {}class NeedsCleanup2 extends NeedsCleanup {  // Construction can fail:  public NeedsCleanup2() throws ConstructionException {}}public class CleanupIdiom {  public static void main(String[] args) {    // Section 1:    NeedsCleanup nc1 = new NeedsCleanup();    try {      // ...    } finally {      nc1.dispose();    }    // Section 2:    // If construction cannot fail you can group objects:    NeedsCleanup nc2 = new NeedsCleanup();    NeedsCleanup nc3 = new NeedsCleanup();    try {      // ...    } finally {      nc3.dispose(); // Reverse order of construction      nc2.dispose();    }    // Section 3:    // If construction can fail you must guard each one:    try {      NeedsCleanup2 nc4 = new NeedsCleanup2();      try {        NeedsCleanup2 nc5 = new NeedsCleanup2();        try {          // ...        } finally {          nc5.dispose();        }      } catch(ConstructionException e) { // nc5 constructor        System.out.println(e);      } finally {        nc4.dispose();      }    } catch(ConstructionException e) { // nc4 constructor      System.out.println(e);    }  }} /* Output:NeedsCleanup 1 disposedNeedsCleanup 3 disposedNeedsCleanup 2 disposedNeedsCleanup 5 disposedNeedsCleanup 4 disposed*///:~
0 0