File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Please can you help me out with this fairly basic question...
I have several classes that do database queries, and in each method I need to try and catch for every SQLException that can happen. Instead of doing this can I create my own SQLException class that will just print out errors to the output screen. Then each method in the other classes can just throw the SQLException and won't need lots of try and catches.
The problem is that I can't get this to work! If it is possible to do this, or something like this, please can you tell me how.
Thanks very much in advance.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Peter
You can subclass SQLException if you need to, just create your own Exception that extends SQLException. But the JDBC methods are still going to throw SQLExceptions not one of yours. You can catch the SQLException and then throw a new one of your Exceptions but you'll still need the try-catch blocks.
The only way around it is if you declare all of those methods to throw a SQLException then it will be the caller that needs to handle it.
If the methods throwing the exception don't really need to catch them then the caller can and handle it as needed. If there is a common caller then you can just have all of the exceptions end up there and get handled in that method/class.
If all you want is to print the errors to the screen just make a method that does that in one of the superclasses you created and send all of the SQLExceptions there - but the only way to do that is to catch them then call the method to print them.
The point is that no matter what you do you have to catch them eventually.
hope that helped a little bit.


Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling