<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [異常與記錄日志](https://lingcoder.gitee.io/onjava8/#/book/15-Exceptions?id=%e5%bc%82%e5%b8%b8%e4%b8%8e%e8%ae%b0%e5%bd%95%e6%97%a5%e5%bf%97) 你可能還想使用 java.util.logging 工具將輸出記錄到日志中。基本的日志記錄功能還是相當簡單易懂的: ~~~ // exceptions/LoggingExceptions.java // An exception that reports through a Logger // {ErrorOutputExpected} import java.util.logging.*; import java.io.*; class LoggingException extends Exception { private static Logger logger = Logger.getLogger("LoggingException"); LoggingException() { StringWriter trace = new StringWriter(); printStackTrace(new PrintWriter(trace)); logger.severe(trace.toString()); } } public class LoggingExceptions { public static void main(String[] args) { try { throw new LoggingException(); } catch(LoggingException e) { System.err.println("Caught " + e); } try { throw new LoggingException(); } catch(LoggingException e) { System.err.println("Caught " + e); } } } ~~~ 輸出為: ~~~ ___[ Error Output ]___ May 09, 2017 6:07:17 AM LoggingException <init> SEVERE: LoggingException at LoggingExceptions.main(LoggingExceptions.java:20) Caught LoggingException May 09, 2017 6:07:17 AM LoggingException <init> SEVERE: LoggingException at LoggingExceptions.main(LoggingExceptions.java:25) Caught LoggingException ~~~ 靜態的 Logger.getLogger() 方法創建了一個 String 參數相關聯的 Logger 對象(通常與錯誤相關的包名和類名),這個 Logger 對象會將其輸出發送到 System.err。向 Logger 寫入的最簡單方式就是直接調用與日志記錄消息的級別相關聯的方法,這里使用的是 severe()。為了產生日志記錄消息,我們欲獲取異常拋出處的棧軌跡,但是 printStackTrace() 不會默認地產生字符串。為了獲取字符串,我們需要使用重載的 printStackTrace() 方法,它接受一個 java.io.PrintWriter 對象作為參數(PrintWriter 會在[附錄:I/O 流](https://lingcoder.gitee.io/onjava8/#/./Appendix-IO-Streams)一章詳細介紹)。如果我們將一個 java.io.StringWriter 對象傳遞給這個 PrintWriter 的構造器,那么通過調用 toString() 方法,就可以將輸出抽取為一個 String。 盡管由于 LoggingException 將所有記錄日志的基礎設施都構建在異常自身中,使得它所使用的方式非常方便,并因此不需要客戶端程序員的干預就可以自動運行,但是更常見的情形是我們需要捕獲和記錄其他人編寫的異常,因此我們必須在異常處理程序中生成日志消息; ~~~ // exceptions/LoggingExceptions2.java // Logging caught exceptions // {ErrorOutputExpected} import java.util.logging.*; import java.io.*; public class LoggingExceptions2 { private static Logger logger = Logger.getLogger("LoggingExceptions2"); static void logException(Exception e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); logger.severe(trace.toString()); } public static void main(String[] args) { try { throw new NullPointerException(); } catch(NullPointerException e) { logException(e); } } } ~~~ 輸出結果為: ~~~ ___[ Error Output ]___ May 09, 2017 6:07:17 AM LoggingExceptions2 logException SEVERE: java.lang.NullPointerException at LoggingExceptions2.main(LoggingExceptions2.java:17) ~~~ 還可以更進一步自定義異常,比如加入額外的構造器和成員: ~~~ // exceptions/ExtraFeatures.java // Further embellishment of exception classes class MyException2 extends Exception { private int x; MyException2() {} MyException2(String msg) { super(msg); } MyException2(String msg, int x) { super(msg); this.x = x; } public int val() { return x; } @Override public String getMessage() { return "Detail Message: "+ x + " "+ super.getMessage(); } } public class ExtraFeatures { public static void f() throws MyException2 { System.out.println( "Throwing MyException2 from f()"); throw new MyException2(); } public static void g() throws MyException2 { System.out.println( "Throwing MyException2 from g()"); throw new MyException2("Originated in g()"); } public static void h() throws MyException2 { System.out.println( "Throwing MyException2 from h()"); throw new MyException2("Originated in h()", 47); } public static void main(String[] args) { try { f(); } catch(MyException2 e) { e.printStackTrace(System.out); } try { g(); } catch(MyException2 e) { e.printStackTrace(System.out); } try { h(); } catch(MyException2 e) { e.printStackTrace(System.out); System.out.println("e.val() = " + e.val()); } } } ~~~ 輸出為: ~~~ Throwing MyException2 from f() MyException2: Detail Message: 0 null at ExtraFeatures.f(ExtraFeatures.java:24) at ExtraFeatures.main(ExtraFeatures.java:38) Throwing MyException2 from g() MyException2: Detail Message: 0 Originated in g() at ExtraFeatures.g(ExtraFeatures.java:29) at ExtraFeatures.main(ExtraFeatures.java:43) Throwing MyException2 from h() MyException2: Detail Message: 47 Originated in h() at ExtraFeatures.h(ExtraFeatures.java:34) at ExtraFeatures.main(ExtraFeatures.java:48) e.val() = 47 ~~~ 新的異常添加了字段 x 以及設定 x 值的構造器和讀取數據的方法。此外,還覆蓋了 Throwable. getMessage() 方法,以產生更詳細的信息。對于異常類來說,getMessage() 方法有點類似于 toString() 方法。 既然異常也是對象的一種,所以可以繼續修改這個異常類,以得到更強的功能。但要記住,使用程序包的客戶端程序員可能僅僅只是查看一下拋出的異常類型,其他的就不管了(大多數 Java 庫里的異常都是這么用的),所以對異常所添加的其他功能也許根本用不上。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看