This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Limited exceptions in the given interface's thrown clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Limited exceptions in the given interface Watch "Limited exceptions in the given interface New topic
Author

Limited exceptions in the given interface's thrown clause

Jin Zhang
Greenhorn

Joined: Jan 15, 2004
Posts: 28
Hi Ranchers,

Some methods of my given interface only throws limited exceptions, such as:

public void update(int recNo, String[] data, long lockCookie)
throws RecordNotFoundException, SecurityException {}

If I want to throw other exceptions, what should I do? I am asking this because that my implementating codes throws IOException. I do not know how to handle this IOException. Catch it and do nothing is obviously not good. I do not want to catch it and create a new RecordNotFoundException or SecurityException eigher. But I cannot throw any other checked exception due to the given interface signature.

Finally I created my own exception extending RuntimeException. Then in my calling code I catch this RuntimeException. I do not think this is a good design either. But do I have better choice?

Thanks.
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi Jin:
I have the same problem. There are two way to deal with this issue. You can throw an Exception of type RuntimeException, or you can wrap the IOExcepiton in a RecordNotFoundException providing the proper message, and using Exception chaining.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
And the first option was considered to be more natural(thus more acceptable?) based on previous discussions(which I could not find as I mentioned to Andy Zhu in this thread

I think this answers part of Andy's question too.

Bing
Jin Zhang
Greenhorn

Joined: Jan 15, 2004
Posts: 28
Thanks all. I will stick my RuntimeException approach.
James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
I have the same issue, I am under the impression is it ok to modify the interface only to add on extra exceptions.

If I am wrong, please let me know.

Thanx

Jarvis


James<br />SCJP 1.4 - 92%<br />SCJD - 93%<br />SCWCD 1.4 - 95%<br />SCBCD 1.3 - 100%<br />SCEA - 92%
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Jarvis,

I am under the impression is it ok to modify the interface only to add on extra exceptions.


My instructions state that my Data class must implement the interface I am given. I don't think they joke when they say that failing to fulfil all 'must' requirements will automaticaly result in a fail.

Sun's test scripts will be set up to expect an implementation of this interface, and their test methods will not expect to have to deal with any additional exceptions - their test scripts (and your exam) would fail.

Cheers,

Jon


SCJD, SCEA
Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
If the user can't find the record, it can be of varios reasons - record not found between other records, record marked as deleted, or record can not be read. Those are different causes of the same problem. I wrapped the IOException in RecordNotFoundException. It makes sense to me.


Regards,<br />Baruch.<p>SDFWOF<br />FGEHWS<br />FNEVGE
James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
I surpose, if more exception are needed, for example RemoteException for the remote DB instance over RMI, it may be possible (depending on your design) to subclass the interface for this purpose.

Jarvis
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Jarvis,

You cannot throw new exceptions from a subclass (or subinterface) not declared in a superclass/interface (or the subclasses of these Exceptions). You can declare a superinterface to throw more exceptions though, making the Sun interface a subinterface (a subinterface/class doesn't need to throw all exceptions of the superclass/interface).

Cheers,

Jon
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Limited exceptions in the given interface's thrown clause
 
Similar Threads
URLyBird 1.2.1 - Exceptions thrown from suncertify.db.Data
a question about the interface
IOException in db find method
B&S Data exceptions
B&S 2.2.2 - Network Layer Query