I know, that finally block is always executed even if exception occurs.
It doesnt execute if we use System.exit(0) in try or catch block;
It is used for releasing resources.
But I have question, the statements after catch block will anyways execute even if those are written without finally, right?
Please explain me.
See the following code snippets -
public static void main(String[] args) throws SQLException {
Connection con=null;
try {
String url ="someURL";
String user ="someUserName";
String password ="somePassword";
con=DriverManager.getConnection(url, user, password);
.
.
.
} catch(Exception e) {
e.printStackTrace();
} finally {
if(con!=null) {
con.close();
}
}
}
and
public static void main(String[] args) throws SQLException {
Connection con=null;
try {
String url ="someURL";
String user ="someUserName";
String password ="somePassword";
con=DriverManager.getConnection(url, user, password);
.
.
.
}catch(Exception e) {
e.printStackTrace();
}
if(con!=null) {
con.close();
}
}
So my con.close();
will execute anyways, then why I need finally?