Solved

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

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

Title # Comments Views Activity
egit plugin on eclipse 8 98
servlet example 11 56
Setup GlassFish 4 35
restrict decimal places for double datatype 10 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

756 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