This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubts" Watch "Doubts" New topic
Author

Doubts

Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
a) Is it mandatory to have a defualt label in switch construct.
b)public class Test
{
static String j = "";
public static void method( int i)
{
try
{
if(i == 2)
{
throw new Exception();
}
j += "1";
}
catch (Exception e)
{
j += "2";
return;
}
finally
{
j += "3";
}
j += "4";
}
public static void main(String args[])
{
method(1);
method(2);
System.out.println(j);
}
}
In the above code why doesn't the statement after finally gets executed. In what scenario does the statement after finally is not executed.
Please explain.
TIA
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
The output should be 13423, so the statement after the finally block, namely, j+="4" is not executed, when i==2.
When i==2 method() throws an exception inside a try block so it will be caught. Inside the catch block the function returns, so all subsequent code in method() will not be executed except for the finally block.
In what scenario does the statement after finally is not executed.

I took the liberty of tweaking your code a bit :

Output should be 1342, bypassing the finally block when i==2.
-anthony
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Oh I forgot...!
a) Is it mandatory to have a defualt label in switch construct.

No.
-anthony
Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
Thanks Anthony!
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Whoops! I misunderstood the question
In what scenario does the statement after finally is not executed.

I thought "In what scenario does the statement in finally is not executed." Sorry
Okay, the statement after finally is not executed when:
(1) When your method returns inside a try, catch or finally block without thrrowing an exception, as we've done, and,
(2) when an exception is thrown inside a try, catch or finally block without being caught, and it propagates outwards.
-anthony
[ April 01, 2002: Message edited by: Anthony Villanueva ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts
 
Similar Threads
flow control
Enthuware questions
Unreachable, return in finally or catch
Exception
COnfusion About Unreachable Code