File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try {} catch() {} finally {} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "try {} catch() {} finally {} " Watch "try {} catch() {} finally {} " New topic

try {} catch() {} finally {}

Andrey Opanasets

Joined: Aug 14, 2001
Posts: 17
Which of these statements are true. Select all correct answers.

1. For each try block there must be at least one catch block defined.

2. A try block may be followed by any number of finally blocks.

3. A try block must be followed by at least one finally or catch block.

4. If both catch and finally blocks are defined, catch block must precede the finally block

My answer id 4. But test says that the answer 2 is also correct. Why? This code is not compliled:


int i=0;

int j=1/i;

} catch(Exception e) {

} finally {


} finally {



How can I defined two or more finally blocks?
Thanx to all.
Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
I AM WITH U... A/C to me option 2 is NOT Correct!
BTW Which test u r using.

Count the flowers of ur garden,NOT the leafs which falls away!

Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining
Andrey Opanasets

Joined: Aug 14, 2001
Posts: 17
I am sorry. Test say 3 and 4.
Can you lock this topic.
vinay jain

Joined: Nov 07, 2001
Posts: 27
The Answers should be 3 and 4, since a try block can not stand on its own e.g. If you have
try {
// do something
and there is no catch or finally block then it is an error.
Madan, Gopal
Ranch Hand

Joined: Aug 13, 2001
Posts: 86
Can somebody explain why option 1 is not true!!??
1. For each try block there must be at least one catch block defined.

I thought this is valid!!

[This message has been edited by Madan, Gopal (edited November 07, 2001).]
Salamina Daniel
Ranch Hand

Joined: Oct 10, 2001
Posts: 41
The reason is obvious, Madan.
You can have a try - finally block whitout any catch involved and Java Language Specification -14.19 gives this description
try Block Catches
try Block Catches-opt! Finally
So, as you can see the catch block is optional !
public class ATest{
public static void main(String arg[]) throws IOException{
FileInputStream f=new FileInputStream("c:\test.txt");
System.out.println("No catch block");
Therefore answer 1.(For each try block there must be at least one catch block defined.) is wrong !
Best regards

[This message has been edited by Salamina Daniel (edited November 07, 2001).]
I agree. Here's the link:
subject: try {} catch() {} finally {}
It's not a secret anymore!