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
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许抛出了一个错误。这仍然会导致 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.
尝试捕获 Throwable,并观察堆栈跟踪,也许
conection.connect()
抛出一个Error
(或其他也扩展的自定义类)可抛出
)。try to catch
Throwable
, and watch stacktrace, maybeconection.connect()
threw anError
(or other custom class which also extendsThrowable
).如果发生 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.
根据
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 extendjava.lang.Exception
. Try to catchjava.lang.Throwable
instead