*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Qn. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Qn." Watch "Exception Qn." New topic
Author

Exception Qn.

Divakar
Greenhorn

Joined: Mar 01, 2000
Posts: 25
This is a question from IBM exam.
With respect to Exceptions, RuntimeExceptions and Errors:

a) All may be caught using try {...} catch () {...} syntax.

b) Developers cannot create their own Error subclasses.

c) A RuntimeException must always be caught or declared as thrown by the method which raises it.

d) Non-RuntimeExceptions must always be caught or declared as thrown by the method which raises them.

e) Errors must always be caught or declared as thrown by the method which raises them.

My answer is (d)
Can Errors be caught using try catch block??
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
a) All may be caught using try {...} catch () {...} syntax. // YES (assuming here ALL means Exception and RuntimeException as u said. Note Exception INCLUDES RuntimeException)
b) Developers cannot create their own Error subclasses. //NO
c) A RuntimeException must always be caught or declared as thrown by the method which raises it. //NO
d) Non-RuntimeExceptions must always be caught or declared as thrown by the method which raises them. //YES (also called as CHECKED EXCEPTIONS)
e) Errors must always be caught or declared as thrown by the method which raises them. //NO
Now what do you think about the foll. code? Compile? Run? or Both? If runs successfully what do you think will bw printed?
regds
maha anna
<pre>
try{
throw new UnknownError();
}catch (UnknownError e) {
System.out.println(e);
}finally{
System.out.println("finally");
}
System.out.println("END");
throw new UnknownError();
</pre>

[This message has been edited by maha anna (edited March 09, 2000).]
Divakar
Greenhorn

Joined: Mar 01, 2000
Posts: 25
This code compiles and while running error thrown at try block is caught by catch.
All the confusion arised from the Sun java documentation for Errors.
They have given
"An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. "

mary jacobson
Greenhorn

Joined: Apr 27, 2005
Posts: 7
Hi divakar,
I copied and tried the above two programs. I getting compile error for both of them.I am using jdk 1.2.2.
Thanks.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
AS you would see, that here it is also referring to errors. Sun recommends not to catch errors. So I would say 1 is false.
Originally posted by maha anna:
a) All may be caught using try {...} catch () {...} syntax. // YES (assuming here ALL means Exception and RuntimeException as u said. Note Exception INCLUDES RuntimeException)
b) Developers cannot create their own Error subclasses. //NO
c) A RuntimeException must always be caught or declared as thrown by the method which raises it. //NO
d) Non-RuntimeExceptions must always be caught or declared as thrown by the method which raises them. //YES (also called as CHECKED EXCEPTIONS)
e) Errors must always be caught or declared as thrown by the method which raises them. //NO
Now what do you think about the foll. code? Compile? Run? or Both? If runs successfully what do you think will bw printed?
regds
maha anna
<pre>
try{
throw new UnknownError();
}catch (UnknownError e) {
System.out.println(e);
}finally{
System.out.println("finally");
}
System.out.println("END");
throw new UnknownError();
</pre>

[This message has been edited by maha anna (edited March 09, 2000).]

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
What you say is correct.It is not a good idea to catch Error. Because they occur due to something seriously wrong happened. Practically it should not happen. If happens we have to address the problem. Instead we should not try to catch the Error. First of all you do not know when it will happen or in which line of code it will happen. Then where can we try to catch the Error ?(except trying to clutter the code..).
But What I thought the intent of the qstn was whether we can/can not catch the Error (if at all we know where to catch). So I thought the answer is true.
regds
maha anna
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception Qn.
 
Similar Threads
About Runtime Exceptions and Errors
Exceptions
Help me solve this
Help me friends. I'll appreciate your answer.(Exceptions)
IBM mock