Solved

y new Exception extends SQLException can't cover all SQLException case?

Posted on 2002-07-13
4
664 Views
Last Modified: 2008-01-16
Dear experts,
  I design a new Exception which extends SQLException. Firstly, I expect it can cover all SQLException case. But it makes me disappointed. :(

This is my new class :
public class MyNewSQLException extends SQLException {...}

I've a a function need to be put inside a SQLException block.

try {
  needSQLExceptionFunction();
} catch (SQLException e) {}

But I hope the MyNewSQLException can cover all SQLException cases, like that :

try {
  needSQLExceptionFunction();
} catch (MyNewException e) {}

  The Java compiler prompts error to do it. Please tell me what's going on and how to do it to match my expectation.

Thank you. :)
0
Comment
Question by:JavaSlave
4 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 7151416
Please post the piece of code and the compiler message.
0
 
LVL 3

Accepted Solution

by:
gandalf94305 earned 50 total points
ID: 7151658
Ahem... you made a sub-class, right? The sub-class will refine the behaviour of the super-class. Therefore, MySQLException will NOT cover all cases of SQLExceptions being thrown. But catching SQLException will also catch all cases where a MySQLException is thrown, just because SQLException is a SUPER-class of MySQLException.

You should change your code to do one of the following:

try {
  //...do some SQL stuff...
} catch (MySQLException e) {
  handleMySQLException(e);
} catch (SQLException e) {
  handleSQLException(e);
}

This will treat cases in which your special MySQLException has been thrown differently from cases where the normal SQLException has been thrown.

Now, if you intend to replace the SQLException by something different, I would like to know what you have in mind. What is it that you are trying to achieve?

If you want to add more information to the exception, you probably have to rewrite JDBC drivers yourself to include that information. If you write a JDBC driver, anyway, the distinction between the generic SQLException and your specific MySQLException will help while preserving compatibility with the existing exception model.

If you need to pass sophisticated information between the thrower and the catcher of your exception, use the following trick: in a class somewhere with static methods/data, keep a Hashtable of threads and some data associated. This class would provide a method setThreadData(Object data) simply storing the argument in a global static hashtable. The thrower will call this method to attach some extra data to the thread. The catcher may check the global hashtable to see if anything has been left there...

I hope this helps.

Cheers,
--gandalf.
0
 
LVL 5

Expert Comment

by:vemul
ID: 7887899
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to gandalf94305

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7959647
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question