當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LogRecord setThrown()用法及代碼示例


java.util.logging.LogRecord的setThrown()方法用於與日誌事件相關聯的throwable。此方法用於在logRecord中記錄可用於記錄消息的異常。

用法:

public void setThrown(Throwable thrown)

參數:此方法接受throw作為可拋出對象的參數。也可以為null。


返回:此方法不返回任何內容。

以下示例程序旨在說明setThrown()方法:
示例1:

// Java program to illustrate setThrown() method 
  
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.LogRecord; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create LogRecord object 
        LogRecord logRecord 
            = new LogRecord(Level.SEVERE, 
                            "Hello Logger"); 
  
        // set throwable object 
        logRecord.setThrown( 
            new IOException( 
                "Error in Input")); 
  
        // print the method name 
        System.out.println( 
            "throwable object Message = "
            + logRecord.getThrown() 
                  .toString()); 
    } 
}
輸出:
throwable object Message = java.io.IOException: Error in Input

示例2:

// Java program to illustrate setThrown() method 
  
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.LogRecord; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create LogRecord object 
        LogRecord logRecord 
            = new LogRecord(Level.SEVERE, 
                            "Hello Logger"); 
  
        // create a throwable object 
        Exception exception 
            = new ArithmeticException( 
                "divide by 0"); 
  
        // set throwable object 
        logRecord.setThrown(exception); 
  
        // print the result 
        System.out.println( 
            "throwable object = "
            + logRecord.getThrown() 
                  .toString()); 
    } 
}
輸出:
throwable object = java.lang.ArithmeticException: divide by 0

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/logging/LogRecord.html#setThrown(java.lang.Throwable)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LogRecord setThrown() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。