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?
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
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.
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.
Joined: Feb 20, 2003
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).
subject: Limited exceptions in the given interface's thrown clause