This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

try {} catch() {} finally {}

Andrey Opanasets
Greenhorn

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:

try{

int i=0;

int j=1/i;

} catch(Exception e) {

} finally {

System.out.println(1);

} finally {

System.out.println(2);

}

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.
Bye.
Viki.

------------------
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 http://groups.yahoo.com/group/IBM340Exam/
Andrey Opanasets
Greenhorn

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

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
<pre>
try {
// do something
}
...
</pre>
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
TryStatement:
try Block Catches
try Block Catches-opt! Finally
So, as you can see the catch block is optional !
Example:
import java.io.*;
public class ATest{
public static void main(String arg[]) throws IOException{
try{
FileInputStream f=new FileInputStream("c:\test.txt");
}
finally{
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).]
 
GeeCON Prague 2014
 
subject: try {} catch() {} finally {}