Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-07-13
4
Medium Priority
?
690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 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