To implement Interface DBAccess, RecordNotFoundException, SecurityException and DuplicateKeyException Classes must be created since they are unimplemented.
My Question is Because they are declared in throws clause of methods, should they all extend Exception class and become checked Exception?? Or In previous threads, some argue SecurityException and DuplicateKeyException should extend RuntimeException. But I didn't read any reason for this.
Definition of RuntimeException: A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
Definition of "Exception" class: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
[ September 14, 2004: Message edited by: Steve Taiwan ] [ September 14, 2004: Message edited by: Steve Taiwan ]
To implement Interface DBAccess, RecordNotFoundException, SecurityException and DuplicateKeyException Classes must be created since they are unimplemented.
My Question is Because they are declared in throws clause of methods, should they all extend Exception class and become checked Exception?? Or In previous threads, some argue SecurityException and DuplicateKeyException should extend RuntimeException. But I didn't read any reason for this.
Definition of RuntimeException: A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
Definition of "Exception" class: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
[ September 14, 2004: Message edited by: Steve Taiwan ]
[ September 14, 2004: Message edited by: Steve Taiwan ]
The RecordNotFoundException should be a checked exception, the SecurityException is not so certain, it could be a checked exception or it could be java.lang.SecurityException which is an unchecked runtime exception. The much larger and uglier question is how to handle IOException, which will occur and isn't thrown by any of the DBAccess methods. There has been a lot of discussion of this topic on this forum, my vote is with the "wrap it in a runtime exception" camp. You can use the Throwable.cause mechanism to pass the actual exception through several layers that know nothing about it.
I prefer making my own exception classes for these 3 exception. And you suggest: 1. RecordNotFoundException: extend "Exception" for checed excpetion. 2. SecurityException: extend "Exception" for checed excpetion.
============================================= My question is: 1. What is your opinion about DuplicateKeyException? 2. What are your reasons for checked or runtime exception?
I prefer making my own exception classes for these 3 exception. And you suggest: 1. RecordNotFoundException: extend "Exception" for checed excpetion. 2. SecurityException: extend "Exception" for checed excpetion.
============================================= My question is: 1. What is your opinion about DuplicateKeyException? 2. What are your reasons for checked or runtime exception?
Thank you, petter.
I defined all those exceptions as checked exceptions in the db package. I don't use them to wrap IOException, since there is no available exception for the find method and that approach isn't consistent. Why should an IOException be a RecordNotFoundException on read and DuplicateKeyException on create?
I define a DataException as a runtime exception and use that to wrap IOException. This way I don't need to modify the interface and RuntimeException is usually indicative of a fatal error.
Steve Taiwan
Ranch Hand
Joined: Jul 01, 2003
Posts: 166
posted
0
Dear Petter.
Thank you for the reply. Your idea about how to wrap IOException is also very good. I will take your solutions and DataException into consideration when I redesign my project.
HSUAN CHEN
Greenhorn
Joined: Sep 24, 2004
Posts: 2
posted
0
Hi peter i have a question about your post, hope you can help me ,thank you
peter define a DataException as a runtime exception and use that to wrap IOException. This way I don't need to modify the interface and RuntimeException is usually indicative of a fatal error.
that will implements be.. if i made some mistake please tell me public synchronized String[] read(int recNo) throws RecordNotFoundException { try { // file access } catch (FileNotFoundException ex) {throws new DataExcetion()} catch (IOException ex) {throws new DataExcetion()} }
peter wooster
Ranch Hand
Joined: Jun 13, 2004
Posts: 1033
posted
0
Originally posted by HSUAN CHEN: Hi peter i have a question about your post, hope you can help me ,thank you
that will implements be.. if i made some mistake please tell me public synchronized String[] read(int recNo) throws RecordNotFoundException { try { // file access } catch (FileNotFoundException ex) {throws new DataExcetion()} catch (IOException ex) {throws new DataExcetion()} }
Thats the right idea, except that the code should read
My changes are: - different variable names need for exceptions - throw not throws - use exception chaining - follow Sun coding convention [ September 28, 2004: Message edited by: peter wooster ]
HSUAN CHEN
Greenhorn
Joined: Sep 24, 2004
Posts: 2
posted
0
Hi peter Thanks for your reply.....i still have some confused: plz help
peter My changes are: - different variable names need for exceptions - throw not throws - use exception chaining - follow Sun coding convention
question are 1-public synchronized String[] read(int recNo) throws RecordNotFoundException but if we never throw RecordNotFoundException ,the code is ok with that?? or change the DataException to RecordNotFoundException ??
public synchronized String[] read(int recNo) throws RecordNotFoundException { try { // file access } catch (FileNotFoundException ex) { throw new RecordNotFoundException "file not found", ex); } catch (IOException ie) { throw new DataException("error reading", ie); }}
2-It is embarrassed that i am poor about the exception chaining would you plz explain more detail for me ex ...class DataException extends RuntimeException { //how to construct DtatException }
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.