📌  相关文章
📜  Java中的 LogRecord setThrown() 方法及示例

📅  最后修改于: 2022-05-13 01:54:41.163000             🧑  作者: Mango

Java中的 LogRecord setThrown() 方法及示例

Java.util.logging.LogRecordsetThrown()方法用于与日志事件关联的 throwable。这用于在 logRecord 中记录可用于记录消息的异常。

句法:

public void setThrown(Throwable thrown)

参数:此方法接受 throwable 作为参数,该参数是可抛出的对象。它也可以为空。

Return :此方法不返回任何内容。

下面的程序说明了 setThrown() 方法:
方案一:

// 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

方案二:

// 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: Java Java)