Weird bug in Java try-catch-finally
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 ");
LO开发者_如何学编程GGER.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
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.
try to catch Throwable
, and watch stacktrace, maybe conection.connect()
threw an Error
(or other custom class which also extends Throwable
).
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.
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
精彩评论