繁体   English   中英

Java 中的奇怪错误 try-catch-finally

[英]Weird bug in Java try-catch-finally

我正在使用 JODConverter 将.xls 和.ppt 转换为.pdf 格式。 为此,我有类似的代码

try{
    //do something
    System.out.println("connecting to open office");
    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    System.out.println("connection object created");
    connection.connect();
    System.out.println("connection to open office successful");
    //do something
    if(!successful)
      throw new FileNotFoundException();
}catch(Exception e){
   System.out.println("hello here");
   System.out.println("Caught Exception while converting to PDF ");
   LOGGER.error("Error in converting media" + e.getMessage());
   throw new MediaConversionFailedException();
}finally{
   decode_pdf.closePdfFile();
   System.out.println("coming in finally");
  //do something here
}

我的 Output:

connecting to open office
connection object created
coming in finally

PS 方法的返回类型为void

这怎么可能? 即使connection.connect() 中存在一些问题,它也会出现在catch 块中。 使困惑

也许抛出了一个错误。 这仍然会导致 try 块未完成,catch Exception 块被忽略并调用 finally 块。

尝试捕捉Throwable ,并观察堆栈跟踪,也许conection.connect()抛出Error (或其他自定义 class 也扩展了Throwable )。

如果发生了 Error 类型的错误,或者更糟的是 Throwable 类型的错误,那么您的 Exception 捕获处理程序将不会触发。 您是否有可能遇到某种 VM 错误、OOM 或堆栈溢出?

如果是这样,这将解释您报告的 output。

根据OpenOfficeConnection接口的实现,可以预期各种类型的 throwable。 这些 throwable 之一可能不会扩展java.lang.Exception 尝试抓住java.lang.Throwable代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM