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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
This is the question of Jtest mocktest (question no 54).
The answer of this question given is B,C,D.
But according to me correct answer should be A,C,D.
I am opposing B because, if you define 'finally' between 'try' and 'catch', the compiler will give error(like in the code given at the bottom.
So for me the sequence of try,catch,finally matters.
Can anybody make this more clear?

Which of the following statements about try,
catch, and finally are true?
A. A try block must always be followed by a catch
block
B. A try block can be followed either by a catch block
or a finally block, or both
C. A catch block must always be associated with a try
block
D. A finally can never stand on its own (that is, without
being associated with try block)
E. None of these are true

My code:
class javaprog
{
public static void main(String [] a) {
go();
}

static void go(){
try{
throw new Exception();
}
finally{}
catch(Exception e){}
}
}
 
Ranch Hand
Posts: 31
  • 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 about try,
catch, and finally are true?
A. A try block must always be followed by a catch
block
B. A try block can be followed either by a catch block
or a finally block, or both
C. A catch block must always be associated with a try
block
D. A finally can never stand on its own (that is, without
being associated with try block)
E. None of these are true
The rules for try/catch/finally are:
(1) a try block cannot be on its own. It can be with a try block or catch block or both
So you could have a try block followed by a finally block and no catch block:
try{
if (result ==0) {
doThis();
return;
} else {
doThat();
throw new MyException();
}
}
finally {
alwaysDoThis();
}

So A is not a valid option.
B is a valid option since it does not specify the order of
the try, catch, finally blocks (the "or both" part of sentence).
C and D are valid options because they satisfy the rule above.

 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry... i messed up. Here's the rule again:
You always need a try block to use catch or finally. With a try, you must use catch, or finally, or both. You cannot use try on its own..
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah friend,
You are correct.
But we can add on emore thing:
If try, catch and finally are there in a program,
the sequence should be try--catch--finally.
thanks for help
Shikhar
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic