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.
Joined: May 10, 2001
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.