• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

catch

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)catch statment can not catch error.True or False?
2)Try statment must follow either catch or finally statment.True or false?
Thank you.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hans,
1) false. Error is derived from Throwable, like Exception. So you can create a try catch-block, which catches Throwable and you will also get the Errors. If this is good programming is something different, but it is possible. At least that's my opinion..
2) true. You can use a try-catch, a try-finally or a try-catch-finally. But either catch or finally has to existent...
As always, correct me if i'm wrong
cheers
Oliver :-) :-)

Originally posted by Hans Li:
1)catch statment can not catch error.True or False?
2)Try statment must follow either catch or finally statment.True or false?
Thank you.


 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Oliver
a) False
Because errors are unrecoverable, they should not be
caught.
b) True
You have to have either catch or finally after any try block.
Thanks
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.true
2. FALSE!!!
Watch out for the question, guys! Try can't follow catch or finally. It's the other way around. Catch or finally must follow try. (Assuming it's not a typo). If it's a typo, Hans you better be careful or silly things like this might cost you points.
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay,
let Question1 be a typo
But if this really was the question, you're right.
But can you explain, why a catch statement can't catch an Error??? I would like to know that...
cheers
Oliver
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting discussion.
Theoretically Error( or its subclasses ) can be caught by <code>catch</code> since they are <code>Throwable</code>. However since they represent usually unrecoverable conditions, handling them is not recommended.
Read what the API documentation says about Error


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.
A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.



Here's a piece of code that works perfectly fine!..but as I said, don't try this at home!!

Ajith
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's nice Ajit!!
So answer should be
1)false
2)You can use try/catch/finally block. But you can use try block without using either catch/finally.
Am i right Gurus!!!
Correct me if i am wrong!!!
Parag
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by parag bharambe:
That's nice Ajit!!
So answer should be
1)false
2)You can use try/catch/finally block. But you can use try block without using either catch/finally.
Am i right Gurus!!!
Correct me if i am wrong!!!
Parag


Hmm, i'm no sure, if i understood you, BUT to make it clear look at the code below

You CAN'Tuse try only, there has to be either a catch or a finally or both.
Hope that helps
Oliver
reply
    Bookmark Topic Watch Topic
  • New Topic