• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Return from catch block

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Can anyone please tell me if there is a return statement from the catch block, and there is a finally block, is the value from the catch block returned first.
I am little confused as how the code flows in the following program.

public class Test1 {

static int i=0;

public static void main(String[] args) {
System.out.println(meth1());
System.out.println(i);
}

static int meth1()
{

try
{
if(i==0)
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println(i);
return ++i;
}
finally
{
System.out.println(i);
return ++i;
}

}
}
The output is 0 1 2 2.

regards,
Raja
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The i value is first incremented in catch block and then in finally block. BTW why do you want to increment the value in catch and finally instead you could do it after finally block
 
I think she's lovely. It's this tiny ad that called her crazy:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic