Solved

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

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 100
how do i compare an object based on two fields 6 54
web services creation SOAP vs REST 5 41
String array comparison 4 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

777 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