• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Qn.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. "

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic