IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

Java Programming - Exceptions - Discussion

@ : Home > Java Programming > Exceptions > Finding the output - Discussion

Read more:

"Nothing in life is to be feared, it is only to be understood."
- Marie Curie
1. 

What will be the output of the program?

public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Finally" ); 
        } 
    } 
}

[A]. Finally
[B]. Compilation fails.
[C]. The code runs with no output.
[D]. An exception is thrown at runtime.

Answer: Option E

Explanation:

If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances:

  1. An exception arising in the finally block itself.
  2. The death of the thread.
  3. The use of System.exit()
  4. Turning off the power to the CPU.

I suppose the last three could be classified as VM shutdown.


Muthu Kumar said: (Sun, Jul 3, 2011 02:11:30 PM)    
 
How it will runs without catch ?

Jafar said: (Tue, Oct 25, 2011 01:08:33 PM)    
 
How the code works?

Jyoti said: (Thu, Nov 10, 2011 12:15:21 PM)    
 
Thanx for this info..

Praveen.Vaka said: (Mon, Nov 28, 2011 11:19:36 PM)    
 
Try can exist with out any catch.

Praveen.V said: (Mon, Nov 28, 2011 11:48:28 PM)    
 
A try can exist with out any catch block but catch can not exit with out try block.

Sonu Raj said: (Fri, Jun 22, 2012 01:07:41 AM)    
 
If method type is void then how it is returning ?

Sai Kumar said: (Fri, Sep 7, 2012 04:12:00 PM)    
 
return; means it's returning nothing.
So we can just give return;

R.Vinod Kumar said: (Wed, Oct 17, 2012 10:25:01 AM)    
 
Finally block is executing means releasing the resources used.

So always resources must be released after using, So Finally block must be executed compulsorly everytime.

If exception raises in try block, then JVM searches for Particular catch block, if catch block is absent then Finally will be executed to close the resources and then error/exception message will be printed on command prompt.

But here in this case no exception raised in try block. So, everything is fine (means simply Finally executed).

Av.Nikhil said: (Wed, May 8, 2013 10:54:53 PM)    
 
The "return;" statement in try block has to send the control back to the calling method (main) without returning any value (because it allowed only in void methods). So without searching for catch blocks control going to the calling method.

But anyhow finally block should execute before the control goes the main method.

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!