Georgios Tasioulis
Georgios Tasioulis

Reputation: 37

Return in Catch Statement

At the following program, in case I remove the return statement from:

catch(FileNotFoundException e) { 
        System.out.println("File Not Found"); 
        return; 

It shows me an error inside the do while loop, that the local variable fin might not have been initialized. Can someone explain me why is this happening?

import java.io.*; 
class Stock { 
    public static void main(String args[]) 
            throws IOException { 
        int i; 
        FileInputStream fin; 
        try { 
            fin = new FileInputStream(args[0]); 
        } catch(FileNotFoundException e) { 
            System.out.println("File Not Found"); 
            return; 
        } 
        // read characters until EOF is encountered 
        do { 
            i = fin.read(); 
            if(i != -1) System.out.print((char) i); 
        } while(i != -1); 
            fin.close(); 
        } 
}

Upvotes: 1

Views: 270

Answers (5)

Kick Buttowski
Kick Buttowski

Reputation: 6739

Why compiler complain

-  if your main() encounter the exceptions it will pass try catch block and never get the 
    chance to initialize fin variable  
 -  Compiler knows that program may end up with throwing an exception which has high chance
    in order to avoid any fruther issue it will not complie

I advise you to do this

 - use try catch block with resources since Java 7 since Class `FileInputStream` is     `closable`
  - put everything in a good order 

code:

try(FileInputStream fin =new FileInputStream(args[0]) ) { 
          while ((content = fin.read()) != -1) {
            // convert to char and display it
            System.out.print((char) content);
        }
        } catch(FileNotFoundException e) { 
            System.out.println("File Not Found"); 
            return; 
        } ...
....
..

Source:

http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

http://tutorials.jenkov.com/java-exception-handling/try-with-resources.html

Upvotes: 1

Warren Dew
Warren Dew

Reputation: 8928

The exception could have been thrown in the call in the try block, so no value will have been assigned to the variable fin. Without the return statement, the subsequent use of the uninitialized fin variable causes the compiler to complain.

With the return statement, if the fin variable is never initialized, it is never used, either, so the compiler is okay with that.

Upvotes: 1

Samusaaron3
Samusaaron3

Reputation: 532

If an exception is encountered while instantiating the FileInputStream, then control moves to the catch block before a value is stored in the variable fin. Without the return statement in place the program would continue execution into the do/while loop, where a method (read) would be called on the uninitialized variable, resulting in an error.

Upvotes: 0

LVBen
LVBen

Reputation: 2061

An exception could occur in new FileInputStream(args[0]);, which would cause fin to not have been initialized.

Try using a default value for fin:

fin = null;

Then later, check to make sure it is not null:

if (fin != null)
{
   do { 
       i = fin.read(); 
       if(i != -1) System.out.print((char) i); 
      } while(i != -1); 
      fin.close(); 
} 

Upvotes: 0

ajb
ajb

Reputation: 31689

If you remove the return statement, you get:

public static void main(String args[]) 
        throws IOException { 
    int i; 
    FileInputStream fin; 
    try { 
        fin = new FileInputStream(args[0]); 
    } catch(FileNotFoundException e) { 
        System.out.println("File Not Found"); 
    } 
    // read characters until EOF is encountered 
    do { 
        i = fin.read(); 
        if(i != -1) System.out.print((char) i); 
    } while(i != -1); 
        fin.close(); 
    } 

Now if FileInputStream throws an exception, it doesn't return a result, and fin doesn't get initialized. The catch block handles the exception and prints a message, but then the code continues, and the next thing it tries to execute is i = fin.read(). Since the compiler figures out that there's a possible way to get to this statement without initializing fin or assigning anything to it, it won't compile the program.

If you put the return back in, this can't happen, because the catch block will cause main to return, and you can't get to fin.read().

Upvotes: 3

Related Questions