Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubts

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I forgot...!

a) Is it mandatory to have a defualt label in switch construct.


No.
-anthony
 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anthony!
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic