Limited exceptions in the given interface's thrown clause
Jin Zhang
Greenhorn
Joined: Jan 15, 2004
Posts: 28
posted
0
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
posted
0
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
posted
0
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 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
posted
0
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.
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
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Limited exceptions in the given interface's thrown clause