File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Limited exceptions in the given interface's thrown clause

 
Jin Zhang
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Zung
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I will stick my RuntimeException approach.
 
James Turner
Ranch Hand
Posts: 194
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jon Entwistle
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Baruch Sadogursky
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Turner
Ranch Hand
Posts: 194
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic