• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

confused about finally

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test23
{
static String raid ( )
{
try
{
throw new Exception ( ) ;
}
catch ( Exception e )
{
int i = 1 / 0 ;
return " Error " ;
}
finally
{
return " Peace " ;
}
}
public static void main ( String args [ ] )
{
System . out . println ( raid ( ) ) ;
}
}

the output i got is
---------- Javac ----------
Test23.java:17: warning: finally clause cannot complete normally
}
^
1 warning

-------------------------------------------
But in the mock question the answer is given as "Peace " would be printed.

Please help me out in this
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using 1.5, I don't get any warnings. Even so, you should still be able to run this.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I comment out the 'return "Error" ' and or 'return "Peace" ' lines from this program it gives an error:

C:\Java\JavaCert\Test23.java:21: missing return statement
}
^
1 error

Why is the return statement needed here?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't look like you quite have this setup correctly.

You are throwing an error manually within the try, but then have another error in the finally that you are not catching.

Something like this shows the flow a bit better:

static String raid() {
String returnString = null;
try {
throw new Exception();
} catch (Exception e) {
int i = 1 / 0;
returnString = " Error ";
} finally {
returnString = " Peace ";
}
return returnString;
}

The error gets thrown completely.

However, this is a cleaner example of finally...

static String raid() {
String returnString = null;
try {
returnString = " Success ";
int i = 1 / 0;
} catch (Exception e) {
returnString = " Error ";
} finally {
returnString = " Peace ";
}
return returnString;
}

The string starts blank, during the try gets set to " Success", but an error occurs, changing it to " Error", but then the finally block runs, after the error, changing it to " Peace ", which is then returned.
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi karthik,

I am able to compile and run you code in j2sdk1.4..

output is "Peace"....

In the event of a return in try / catch and return also exists in finally...

the return of finally is considered...and the return of try/catch block is

ignored..

Hope you got it!!!

Regards
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi A kumar

i dont understand why peace is printed only .though an exception is been thrown in try and is caught in catch {}.so the return value has to printed ???
 
A Kumar
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,

As i had mentioned....if there is a return in both try or catch and finally... the return of try or catch is ignored...

because if that the return is proceeded with ....then finally cannot be executed..Of course...you can ask that it can execute finally and ignore...the return in finally block and execute the return of try/catch..

But the finally block has a special significant in the sense that it should be executed with or without exceptions..

here i have modified your code so that ...finally doesnt return...




 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic