📄 e388. logging an exception.txt
字号:
The logger method log() can be used to log an exception. Also, the convenience method Logger.throwing() can be used by a method about to throw an exception.
package com.mycompany;
class MyClass {
public void myMethod() {
Logger logger = Logger.getLogger("com.mycompany.MyClass");
// This method should be used when an exception is encounted
try {
// Test with an exception
throw new IOException();
} catch (Throwable e){
// Log the exception
logger.log(Level.SEVERE, "Uncaught exception", e);
}
// When a method is throwing an exception, this method should be used
Exception ex = new IllegalStateException();
logger.throwing(this.getClass().getName(), "myMethod", ex);
}
}
Here is a sample of the output generated by the example:
Jan 11, 2002 5:16:49 PM com.mycompany.MyClass myMethod
SEVERE: Uncaught exception
java.io.IOException
at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:32)
at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18)
Jan 11, 2002 5:16:50 PM com.mycompany.MyClass myMethod
FINER: THROW
java.lang.IllegalStateException
at com.mycompany.MyClass.myMethod(com.mycompany.MyClass.java:25)
at com.mycompany.MyClass.main(com.mycompany.MyClass.java:18)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -