【发布时间】:2011-09-24 21:56:39
【问题描述】:
我正在使用 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
}
我的输出:
connecting to open office
connection object created
coming in finally
附:方法的返回类型是void
这怎么可能?即使connection.connect() 有问题,它也会出现在catch 块中。 困惑
【问题讨论】:
-
尝试捕捉
Throwable,并观察stacktrace,也许conection.connect() 抛出了一个错误(它也扩展了Throwable)。 -
也许你有一个
java.lang.Error? -
为什么不附加一个调试器,看看会发生什么?我的猜测是你缺少一个类/库,所以
connection.connect()导致ClassNotFoundError。这不会被捕获为异常,但它会带您进入finally子句。 -
正确。但是 jvm 仍然会告诉一些有关 ClassNotFoundError 的信息,并且由于本地没有丢失 lib,所以我无法检测到该错误。
标签: java try-catch program-flow jodconverter