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

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

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

Java.lang.reflect.LogRecordgetThrown()方法用于获取与日志事件关联的 throwable。这用于在 logRecord 中记录可用于记录消息的异常。

句法:

public Throwable getThrown()

参数:此方法不接受任何内容。

Return :此方法返回一个 throwable

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

// Java program to illustrate
// getThrown() 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.INFO,
                            "GFG Logger");
        logRecord.setThrown(
            new IOException(
                "Error in Input"));
  
        // get Throwable object
        Throwable throwObj
            = logRecord.getThrown();
  
        // print result
        System.out.println(
            "throwable object = "
            + throwObj
                  .toString());
    }
}
输出:
throwable object = java.io.IOException: Error in Input

方案二:

// Java program to illustrate
// getThrown() method
  
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.WARNING,
                            "Logger");
        logRecord.setThrown(
            new ArithmeticException());
  
        // get Throwable object
        Throwable throwObj
            = logRecord.getThrown();
  
        // print result
        System.out.println(
            "throwable object = "
            + throwObj
                  .toString());
    }
}
输出:
throwable object = java.lang.ArithmeticException

参考资料: https: Java/util/logging/LogRecord.html#getThrown()