4

I have following 2 code snippets and I am wondering what makes java compiler (In Eclipse with Java 7) to show error for 2nd snippet and why not for 1st one.

Here are the code snippets:

snippet 1

public class TestTryCatch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(get());
    }

    public static int get(){
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
        finally{
            System.out.println("In finally...");
            return 2;
        }
    }
}

snippet 2

public class TestTryCatch {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(get());
    }

    public static int get(){
        try{
            System.out.println("In try...");
            throw new Exception("SampleException");
        }catch(Exception e){
            System.out.println("In catch...");
            throw new Exception("NewException");
        }
//      finally{
//          System.out.println("In finally...");
//          return 2;
//      }
    }
}

In eclipse, snippet1 shows to add 'SuppressWarning' for finally block, but in snippet2, it shows to add 'throws or try-catch' block for the throw statement in catch block.

I had a detailed look at following questions, but they didn't provide any concrete reason for this.

Community
  • 1
  • 1
ms_27
  • 1,484
  • 15
  • 22

3 Answers3

2

In snippet 1, you don't need the return statement. In a finally block Java already know what to do once the finally block is treated: either continue an exception handling or jump to the next statement after the finally block.

So in snippet 1, simply move the return statement out of the finally block.

public static int get(){
    try {
        System.out.println("In try...");
        throw new Exception("SampleException");
    } catch(Exception e) {
        System.out.println("In catch...");
        throw new Exception("NewException");
    } finally{
        System.out.println("In finally...");
    }

    return 2;
}

In snippet 2, an exception is thrown in each block. Since this exception is not unchecked, it must be indicated in the method synopsis. Additionnally, there is no return statement and the return value of the method is not void.

Simply add a throws statement at the end of the method with a return statement.

public static void get() throws Exception {
    try{
        System.out.println("In try...");
        throw new Exception("SampleException");
    }catch(Exception e){
        System.out.println("In catch...");
        throw new Exception("NewException");
    }
    //      finally{
    //          System.out.println("In finally...");
    //          return 2;
    //      }
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
2

The 2nd snippet does not have a return value. It has been commented out with the finally clause.

Try this

public static int get(){
    try{
        System.out.println("In try...");
        throw new Exception("SampleException");
    }catch(Exception e){
        System.out.println("In catch...");
        throw new Exception("NewException");
    }
    return 2;   // must return a value
//      finally{
//          System.out.println("In finally...");
//          return 2;
//      }
Mike Murphy
  • 1,006
  • 8
  • 16
2

The finally block should always execute. This is the main reason. The exception is thrown in the catch block but return statement executed in the finally block masks exception. Either remove finally block or move return statement out of the finally block.

  • 1
    Thanks. So, its all about masking. We can say that, finally block will always masks the exception being thrown in catch block. Or, there are some criteria's for this as well. – ms_27 Nov 19 '15 at 09:50
  • @nipkon Then considering following block, each time we run this block the sequence of sysout's and stack trace in output is different. `try{ System.out.println("In try..."); int x = 10/0; }catch(Exception e){ System.out.println("In catch..."); e.printStackTrace(); } finally{ System.out.println("In finally..."); }` – ms_27 Nov 19 '15 at 09:54
  • sorry for above, couldn't make it look pretty in comments. – ms_27 Nov 19 '15 at 09:55