2011-06-29 55 views
3

我使用JODConverter将.xls和.ppt转换为.pdf格式。为此,我有一些代码像Java中的奇怪错误try-catch-finally

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块。 困惑

+4

尝试捕获'Throwable',并观看stacktrace,也许conection.connect()抛出一个错误(这也延伸Throwable)。 – amit

+0

也许你有'java.lang.Error'? – wjans

+1

为什么不附加调试器,看看会发生什么?我的猜测是你缺少一个类/库,所以'connection.connect()'产生一个'ClassNotFoundError'。这不会被当作一个例外,但它会让你进入'finally'子句。 –

回答

5

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

1

如果发生类型为Throwable的类型错误或更糟糕的错误,那么Exception的catch处理程序不会触发。是否有可能得到某种虚拟机错误或OOM或堆栈溢出?

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

2

试图抓住Throwable,看堆栈跟踪,也许conection.connect()抛出Error(或其他自定义这也延伸Throwable类)。

0

根据OpenOfficeConnection的实现接口可以预期各种类型的throwables。这些投掷物中的一个可能不会延伸java.lang.Exception。尝试赶上java.lang.Throwable而不是