This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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

Exceptions

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the use of having a return statement in a catch or finally block?
Basically, how does the rpogram flow, once it encounters a return statement?
For exp, in the program below, what will be the output if "Hello.txt" doesn't exist:


Thanks..
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guptajee,


Posted By Lovleen Gupta
Whats the use of having a return statement in a catch
or finally block? Basically, how does the rpogram flow,
once it encounters a return statement?



Suppose,if exception is thrown, you want to make return statement,
and no other statement execution of the method to proceed. But finally
will execute for sure unless you write System.exit(1) or something that
terminates the program. Suppose you dont write "return -1;" in your catch
clause, even after exception comes, a line that may outside of the try catch
clause will execute and may yield undesirable result. So you wrote "return -1" there.

Got it?


Regards,
cmbhatt
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
Hi Guptajee,



Suppose,if exception is thrown, you want to make return statement,
and no other statement execution of the method to proceed. But finally
will execute for sure unless you write System.exit(1) or something that
terminates the program. Suppose you dont write "return -1;" in your catch
clause, even after exception comes, a line that may outside of the try catch
clause will execute and may yield undesirable result. So you wrote "return -1" there.



Got it?


Regards,
cmbhatt



So, in this program, why is -1 printed and not 0?
Please explain.

Thanks.
[ April 12, 2007: Message edited by: Lovleen Gupta ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Gupta,

System.exit(0); //Normal termination of the program
System.exit(-1); //Supposed to be an abnormal termination of program

But as you return anything from the method, generally -1 shows that something
went wrong. You can even return 3, 13 , and 43 if you treat as it is abnormal
return from the method. (It is upto you). But general practice says negative returned value, something abnormal happened there.


Got it Guptajee?

Regards,
cmbhatt
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic