This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
First of all, you should use the UBB CODE tags to preserve formatting in any code that you post here. It will make it much easier to read
Second, I don't think this code will compile. You have to give names to all the Exception references in the catch statment. For example, you need something like
Third, it's typically considered bad practice to catch Exception. One reason is that such a catch clause will also catch all RuntimeExceptions (such as NullPointerException, IllegalArgumentException, etc.) that should be allowed to propogate up the call stack so the user knows how to deal with them.
Fourth, throwing a new custom exception is usually a good idea. This avoids cluttering your own throws clauses (and the callers catch statements) with multiple exception types. However, your code hides the original cause of the exception. To include information about the original exception, you should use Exception Chaining. Java 1.4 and later implements this automatically with a constructor that takes a "cause" argument. So for example, you can do something like
Finally, it seems to me like you have a lot of try...catch statements in this code. In many places each try...catch statement only contains a single line of executable code. Personally, I would wrap the whole method, or at least groups of related statements, in a single try...catch statement. It looks like the main reason for the "extra" code is so you can give a descriptive error message about what the problem is. But if you use suggestion #4, it preserves the original cause so the descriptive error message isn't quite as important.
This is mostly my own opinion, especially, the last one so take it for the 2 cents that it's worth
Thank you for the advice, I have not posted my whole code previously. I am following all conventions u have listed out. I am posting my code down here.. The requirement is When ever we get an exception or not while closing a Statement, It should not stop us from closing a Connection.
I am following the below steps of closing my database resources. Please correct me if I am wrong in doing so..
Never throw an exception from your finally block, it will mask the exception thrown in the catch block.
I agree that you need to close all objects in order to be certain that you prevent problems such as memory leaks. What I do is to close the ResultSet, Statement and Connection objects in the finally block and write a message to the log if SQLException is thrown.