• 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 and finally

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused by some answers from mock exams. Want help.
My understanding is that the following are valid.
(1)try {//block}; catch(Exception e){//block};
(2)try {//block}; catch(Exception e){//block}; finally {//block};
(3)try {//block}; finally{//block};
(4)in all 1 to 3, can replace catch block with a series catches in the a specific order (more general) is fine;
(5)no try block then no catch block;
(6)no try block then no finally block.
Am I right? Please clarify and add.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the statements seems to be alright just you should not be putting semi-column after the blocks of code.
thanx.
Jennifer.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.try/catch ...finally not mandatory
2.Specific catch first, more general exceptions later
3. return must be the last statement in a try catch finally mechanism. Statements become unreachable(compile time err) if there is any statements declared after return statement
4. if try/catch/finally all the three has return... finally return will override all the above ones
Correct me if i am wrong

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ragu is rigth regarding point 4)
Another point:
try{ /*break or continue or return or throw*/ }
finally{ /*break or continue or return or throw*/ }
This case means the instruction that was going to leave out the try clause is not executed. It would be executed only if finally returns. Note that is possible to lost an important exception thrown in try.
Another similar:
try { /*break or continue or return or throw*/ }
catch{ /*break or continue or return or throw*/ }
finally{ /*break or continue or return or throw*/ }
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ragu Sivaraman:
3. return must be the last statement in a try catch finally mechanism.

If a return makes statements unreachable, it must be the last statement within a catch block. The return is not required.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic