Solved

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

Posted on 2002-07-13
4
653 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now