【问题标题】:How can I change what printStacktrace() does for Exception?如何更改 printStacktrace() 对异常的作用?
【发布时间】:2014-10-04 12:32:40
【问题描述】:

我尝试创建一个扩展异常并覆盖 printstacktrace 的新类。现在的问题是,当它尝试以下类似的操作时,我收到一个错误“无法访问 LoggerException 的 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(); 
}

【问题讨论】:

    标签: java google-app-engine exception


    【解决方案1】:

    你应该扩展 RuntimeException 类

    【讨论】:

    • 是的。但即使这样做也不能帮助您更改 IOException printStackTrace 方法的行为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2016-10-15
    • 2012-03-09
    • 2012-08-16
    • 2021-05-23
    • 2011-04-20
    • 1970-01-01
    相关资源
    最近更新 更多