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 NX: Throwing different exceptions from the interface signature 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 "NX: Throwing different exceptions from the interface signature" Watch "NX: Throwing different exceptions from the interface signature" New topic
Author

NX: Throwing different exceptions from the interface signature

Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Hello
I am having trouble on throwing different exceptions that is specified in the DBAccess interface.
For example:

In this method, how can I throw IOException that is thrown by the seek() and read() methods that I use?


SCJP, SCWCD
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Catch 'em, wrap 'em, and chuck 'em?
[ April 16, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Originally posted by Barry Gaunt:
Catch 'em, wrap 'em, and chuck 'em?
[ April 16, 2003: Message edited by: Barry Gaunt ]

Sorry, I did not understand everything. I catch the IOException, an then? What do you mean with "wrap 'em and chuck 'em"?
Should I use some "setError" passing the IOException, and then who was using that method check if an error occurred calling "getError"? Or should I throw a RuntimeException to alert that an exception occurred?
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
hi Wagner,
I think u should use try-catch block to find IOException,if u haven't declear it in ur method definition.


Nick Lee
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sorry, my post was a little cryptic
You catch the IOException ioe, say, and then do something like: throw new RecordNotFoundException( "IOException in seek", ioe);
Take a look at the Java 1.4 API for Exception and Throwable.
[ April 16, 2003: Message edited by: Barry Gaunt ]
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Originally posted by Barry Gaunt:
Sorry, my post was a little cryptic
You catch the IOException ioe, say, and then do something like: throw new RecordNotFoundException( "IOException in seek", ioe);
Take a look at the Java 1.4 API for Exception and Throwable.
[ April 16, 2003: Message edited by: Barry Gaunt ]

Ok, thank you!
I read the Throawble class javadoc and learned about "chained exception". I think that is just fine for me
Thank you again!
PS: I�m SJCP 1.3 and did not know about "chained exception".
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Originally posted by Nick Lee:
hi Wagner,
I think u should use try-catch block to find IOException,if u haven't declear it in ur method definition.

Nick, the problem was not to try-catch the error, but what to do with that exception. Now I will do something like this:
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Originally posted by Barry Gaunt:
Sorry, my post was a little cryptic
You catch the IOException ioe, say, and then do something like: throw new RecordNotFoundException( "IOException in seek", ioe);
Take a look at the Java 1.4 API for Exception and Throwable.
[ April 16, 2003: Message edited by: Barry Gaunt ]

Hum... I remember one thing, the spec says: "Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file."
So my question: should I throw RecordNotFoundException ONLY if that condition happens (and not when IOException occurrs)?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
So my question: should I throw RecordNotFoundException ONLY if that condition happens?
I will mention in the documentation of my solution that the RecordNotFoundException could be also thrown for other reasons and explain how to determine the exact reason and leave it at that.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Another thought just occurred. In the really bad cases it is possible to throw a RuntimeException, that is an unchecked exception, that does not need to be declared in the throws clause. It is no problem to define our own subclasses of RuntimeException.
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Originally posted by Barry Gaunt:
Another thought just occurred. In the really bad cases it is possible to throw a RuntimeException, that is an unchecked exception, that does not need to be declared in the throws clause. It is no problem to define our own subclasses of RuntimeException.

Hum, acctually I was using RuntimeException . But the idea of define subclasses of RuntimeException looks interesting for me.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
But don't forget RuntimeExceptions are meant for really, really BAD things. Both solutions are possible, depending on the severity of the exception, and what it means to the client.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
I agree here. Let RuntimeExceptions be what they are: all Java aps have tod eal with them. There's no point, IMO, to wrapping them.
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
[ April 17, 2003: Message edited by: Max Habibi ]

Java Regular Expressions
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70

So my question: should I throw RecordNotFoundException ONLY if that condition happens?
I will mention in the documentation of my solution that the RecordNotFoundException could be also thrown for other reasons and explain how to determine the exact reason and leave it at that.

Method create only throws DuplicateKeyException. It seems a little bit uncommon to throw DuplicateKeyException for reasons like IOException. Is it better not to change the interface provided by the instruction? I am thinking adding IOException to these method.
Thanks,
chen
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
My current thinking on this:
In the case of readRecord( recId ) you are looking for a record. If you cannot find it, for any reason, the appropriate exception to be thrown is RecordNotFoundException, possibly wrapping some causing exception. Seeking to a nonexisting record record is an expected cause of an IOException, so it could be caught and changed to a RecordNotFoundException.
In the case of create, DuplicateKeyException (whatever a duplicate key may mean) is very specific, so it is indeed not appropriate to wrap an IOException. Now the problem is: how strict is the interface provided by Sun? If you cannot modify the interface, you cannot add IOException to the throws clause. If during your create record algorithm you encounter an IOException, in that case, a runtime exception could be thrown in place of the IOException. In that case you do not have to declare the runtime exception in method create()'s throws clause.
The point is you have to take each method and make some decision about the most appropriate exception handling. Whatever you decide, make sure to document your decision, with a "change request" if you feel it's necessary.
[ April 18, 2003: Message edited by: Barry Gaunt ]
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Thanks Barry. Your reply really help.
I have sent email to SUN asking if I could add exception to method, so far have not got any answer yet.
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Hi,shan.
I have the same problem with you. If you get any answer from sun, please go on this thread, thx.
Larry.


The hill is not a hill.
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
Hi, Larry,
I just got the reply from SUN. I must implement the interface exactly as it appears in the instructions. So I'd follow Barry's suggestion.
Shan
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
So we should not add any other exceptions and just to
"throws new RuntimeException()" ?
Larry.
[ April 23, 2003: Message edited by: Larry Lee ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From SCJP2, you do not say "throws RuntimeException" because runtime exceptions are unchecked exceptions.
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70
No, we should not add "throws IOException" to method signature if it is not there in the instruction.html.
Shan
Wagner Danda Da Silva Filho
Ranch Hand

Joined: Mar 21, 2003
Posts: 80
Thank you everybody. All this discussion was very helpfull! Thank you again.
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
I still confused with it.
So when IOEeceptions occur, we just catch them and ignore them?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: Throwing different exceptions from the interface signature
 
Similar Threads
Business Delegate pattern problem - code example
NX:about exceptions.
Doubts about throwing exceptions in data access class
Propagating exceptions to client
No definition of what is a DuplicateKeyException