• 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

try/catch question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?

1. A try clause must have a matching catch and finally clause
2. A try clause can be used without a matching finally or catch clause
3. A try clause can be used with a matching finally clause and no catch clause.


My answer was 2. The web site says the correct answer is 3. Can someone please explain?

Thx
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A try clause does not need to have a corresponding catch clause. Just having a finally clause is sufficient. Of course, it is essential to provide atleast one of the two clauses, and yes, you can have both of them as well.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If my understanding of English is good both 2 and 3 are correct.
2. A try clause can be used without a matching finally or catch clause

i.e. try{}finally{} //yes no matching catch
OR try{} catch(Exception e){} //yes no matchin finally

3. A try clause can be used with a matching finally clause and no catch clause.
i.e. try{}finally{} //yes no matching catch
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which of the following statements are true?

1. A try clause must have a matching catch and finally clause
2. A try clause can be used without a matching finally or catch clause
3. A try clause can be used with a matching finally clause and no catch clause.



Key is :
Try clause should be followed by atleast one catch clause or finally clause

1) A try clause must have a matching catch and finally clause

ans : this statement specifies that try should have both.so it is false

2) A try clause can be used without a matching finally or catch clause

ans : this statement specifies that try can occur without both catch and finally so its also false

3)A try clause can be used with a matching finally clause and no catch clause.

ans: this statement specify that try can occur without catch clause and with finally clause.This is true.

Hope now UR clear
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's ambiguous English.


A try clause can be used without a matching finally or catch clause


If you interpret this as:

("try clause" with "!finally clause") or ("try clause" with "!catch clause"), then it is certainly a correct answer.

If you interpret it as:
"try clause with ("!finally clause and !catch clause"), then it is not a correct answer.

You have to guess at the intention of the writer of the question. In this case, you guessed wrong.
 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vidhyasagar. Your explanation clears my doubt.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does matching here means...
i mean in what context you ppl are saying...
and

pls didn't get the 2nd option fully... not satisfied

don't mind Tony.... i appreciate for ur reply but

 
reply
    Bookmark Topic Watch Topic
  • New Topic