It has always been a hot topic either there should be Catch Exception or UnCatch Exception(RuntimeException)
well I am not jumping in to this discussion over here,
But there is a problem with CatchException that it increases lot of line of code, I have did some work around against this issue
which I am sharing with you.
try {
OpenConnection(); // It requires Try-Catch
// Other Logic.....
CloseConnection();
} catch (SQLException e) {
try {
CloseConnection(); // this also requires Try-Catch
} catch (SQLException e1) {}
}
Now Suppose If any-other exception occurs other than SQLException e.g. NullPointerException or any other RuntimeException,
catch block will not be executed and Session/Connection will not be closed creating a Memory Leak.
So to prevent this we can write
try {
OpenConnection(); // It requires Try-Catch
// Other Logic.....
CloseConnection();
} catch (Exception e) {
try {
CloseConnection(); // this also requires Try-Catch
} catch (SQLException e1) {}
}
Now in the above case Exception block will be executed iff the exception which is thrown
is a child of Exception and almost all the Exceptions are Inheriting Exception,
Not All !?
So if we are interested in catching all the Exception then we should do
try {
OpenConnection(); // It requires Try-Catch
// Other Logic.....
CloseConnection();
} catch (Throwable e) {
try {
CloseConnection(); // this also requires Try-Catch
} catch (SQLException e1) {}
}
(Now in any exception, catch block will be executed. However it is not a good way nor a good practise).
But again there is a one more problem, that if anywhere java.lang.Error occurs (it could occur
like ClassNotFound), then even in this scenerio catch block will not be execute.
So the better approch will be
try {
OpenConnection(); // It requires Try-Catch
// Other Logic.....
} catch (SQLException e) {
// do related work ...
}
finally{
try {
CloseConnection(); // this also requires Try-Catch
} catch (SQLException e1) {}
}
By closing the Connection/Session in "finally", in any case "finally" block will be executed before leaving
the "try" block. Hence it can be make sure that Connection will be closed in any case.
Again there is one more issue, it is increasing 4-6 lines of code(in every method), because "closeConnection"
also throws a checked exception and again it requires a try-catch block.
This could be prevented, by doing this way
try {
try{
OpenConnection(); // It requires Try-Catch
// Other Logic.....
}finally{
CloseConnection(); // this also requires Try-Catch
}
} catch (SQLException e) {
// do related work ...
}
More appropiate solution is
try {
Transaction t= null;
try{
t= OpenConnection(); // It requires Try-Catch
// Other Logic.....
CommitTransaction();
}finally{
if(t != null && !t.wasCommitted()) RollbackTransaction();
CloseConnection();
}
} catch (Exception e) {
// do related work ...
}
The above solution has one drawback, if suppose some exception occured in "Other Logic",
then finally block will executed and if some exception occured in finally block also the exception occured in "Other Logic" will be overriden and lost.
No comments:
Post a Comment