• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

NX: Throwing different exceptions from the interface signature

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Catch 'em, wrap 'em, and chuck 'em?
[ April 16, 2003: Message edited by: Barry Gaunt ]
 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Wagner,
I think u should use try-catch block to find IOException,if u haven't declear it in ur method definition.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,shan.
I have the same problem with you. If you get any answer from sun, please go on this thread, thx.
Larry.
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From SCJP2, you do not say "throws RuntimeException" because runtime exceptions are unchecked exceptions.
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody. All this discussion was very helpfull! Thank you again.
 
Larry Li
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still confused with it.
So when IOEeceptions occur, we just catch them and ignore them?
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic