File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RuntimeException or Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RuntimeException or Exception" Watch "RuntimeException or Exception" New topic
Author

RuntimeException or Exception

Steve Taiwan
Ranch Hand

Joined: Jul 01, 2003
Posts: 166
I got B&S.

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.

Thank you for reading my thread.

===================================================================

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 ]

Steve Taiwan<br />SCJP 1.2, SCJD 1.4, SCWCD 1.3, SCBCD 1.3, OCP 8i DBA, SCEA
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Steve Taiwan:
I got B&S.

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.

Thank you for reading my thread.

===================================================================

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.
Steve Taiwan
Ranch Hand

Joined: Jul 01, 2003
Posts: 166
Dear Petter.

Before I post, I have read threads about IOException and SecurityException, I choose:

1. IOException: I wrap it inside RecordNotFoundException.
reasone: "can't read file" means "can't get any record"

2. SecurityException: I go with making a different class from java.lang.SecurityException.
reason: The purpose of SecurityException is completely different from
java.lang.SecurityException. I prefer making a new class.

================================================

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.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Steve Taiwan:
Dear Petter.

Before I post, I have read threads about IOException and SecurityException, I choose:

1. IOException: I wrap it inside RecordNotFoundException.
reasone: "can't read file" means "can't get any record"

2. SecurityException: I go with making a different class from java.lang.SecurityException.
reason: The purpose of SecurityException is completely different from
java.lang.SecurityException. I prefer making a new class.

================================================

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
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
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
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
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
}
 
 
subject: RuntimeException or Exception