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

发布于 2024-11-17 18:58:07 字数 1061 浏览 7 评论 0原文

我正在使用 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
}

My Output:

connecting to open office
connection object created
coming in finally

PS return type of method is void

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

I'm using JODConverter to convert .xls and .ppt to .pdf format. For this i have code something like

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
}

My Output :

connecting to open office
connection object created
coming in finally

P.S. return type of method is void

How is it possible ? Even if there is some problem in connection.connect(), it s'd come in catch block. confused

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朦胧时间 2024-11-24 18:58:07

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

Perhaps an Error was thrown. This would still result in the try block not being completed, the catch Exception block ignored and the finally block being called.

⊕婉儿 2024-11-24 18:58:07

尝试捕获 Throwable,并观察堆栈跟踪,也许 conection.connect() 抛出一个 Error (或其他也扩展 的自定义类)可抛出)。

try to catch Throwable, and watch stacktrace, maybe conection.connect() threw an Error (or other custom class which also extends Throwable).

两相知 2024-11-24 18:58:07

如果发生 Error 类型的错误,或更糟糕的是 Throwable 类型的错误,则 Exception 的 catch 处理程序将不会触发。您是否可能遇到某种虚拟机错误、OOM 或堆栈溢出?

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

If an error of type Error occurred, or worse, of type Throwable, then your catch handler for Exception wouldn't fire. Is it possible that you're getting some sort of VM error, or OOM, or stack overflow?

If so, this would account for the output you reported.

红焚 2024-11-24 18:58:07

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

Depending on the implementation of OpenOfficeConnection Interface various types of throwables can be expected. It is possible that one of those throwables does not extend java.lang.Exception. Try to catch java.lang.Throwable instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文