• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

return statement in try block

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi iam beginer to the java npls tell me the answer i have faced this question in on of the interview.


class demo
{
..........
.........
try
{
...
....
return;
}
catch(....)
{}
finally()
{}
}

In the above program i have written return statement in try block,if exception is there in try block what happend(return is there in try block) and exception is not there in try block?

pls tell me i have faced this question two interviews yaaa
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write a small program yourself, compile it and run it to find out what happens. The best way to learn is to experiment.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you will get an exception it will go to the catch and finally and if no exception then only finally.So return will not effect much.
Best way is write a code and run all aspects,the questions in your mind.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here's the thing that you should burn in when it comes to TRY and CATCH.

1. No matter whether an exception is thrown or not, finally block will be executed!!!.

2. If an exception is thrown, return will not be executed, catch and then finally both will be executed.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the only way to prevent a finally-block from being executed is to call the method:

System.exit();

The fact that this prevents finally from being called may be a reason to throw an exception or an error instead of calling System.exit();

Kaydell
 
pie. 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