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
相关用法
- Java LogRecord setSequenceNumber()用法及代码示例
- Java LogRecord getSourceMethodName()用法及代码示例
- Java LogRecord getInstant()用法及代码示例
- Java LogRecord getSourceClassName()用法及代码示例
- Java LogRecord getParameters()用法及代码示例
- Java LogRecord getSequenceNumber()用法及代码示例
- Java LogRecord getThreadID()用法及代码示例
- Java LogRecord setSourceClassName()用法及代码示例
- Java LogRecord getLoggerName()用法及代码示例
- Java LogRecord setResourceBundleName()用法及代码示例
- Java LogRecord setResourceBundle()用法及代码示例
- Java LogRecord setParameters()用法及代码示例
- Java LogRecord setMillis()用法及代码示例
- Java LogRecord setLoggerName()用法及代码示例
- Java LogRecord setSourceMethodName()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LogRecord setThrown() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。