[英]How do I write the exception from printStackTrace() into a text file in Java?
[英]How can I change what printStacktrace() does for Exception?
我嘗試制作一個擴展Exception並覆蓋printstacktrace的新類。 現在的問題是,當嘗試類似thr以下的操作時,出現錯誤“ LoggerException的Unreachable catch塊。永遠不會從try語句主體中拋出此異常”
公共類LoggerException擴展了Throwable {
private static final long serialVersionUID = 1L;
@Override
public void printStackTrace() {
// TODO Auto-generated method stub
super.printStackTrace();
Logger logger = Logger.getLogger("custom app exception");
logger.log(Level.WARNING,getMessage(),this);
}
}
try {
//do something that does throw anything. but just incase there is a null pointer or something
} catch (LoggerException e) { // error: Unreachable catch block for LoggerException. This exception is never thrown from the try statement body
e.printStackTrace();
}
您應該擴展RuntimeException類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.