Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1006

Posted: Thu Jun 26, 2014 4:02 pm
by sarakh
If we compare this question to enthuware.ocajp.i.v7.2.1276

enthuware.ocajp.i.v7.2.1276:

Code: Select all

public class TestClass{
   public static void main(String[] args){
      TestClass tc = new TestClass();
      try{
         tc.m1();
      }
      catch (MyException e){
         tc.m1();
      }
      finally{
         tc.m2();
      }
   }
   public void m1() throws MyException{
      throw new MyException();
   }
   public void m2() throws RuntimeException{
      throw new NullPointerException();
   }
}
enthuware.ocajp.i.v7.2.1006:

Code: Select all

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
   public static void main(String [] args) throws Exception{
      try{
         m2();
      }
      finally{ m3(); }
    }
    public static void m2() throws NewException{  throw new NewException();  }
    public static void m3() throws AnotherException{  throw new AnotherException();  }
}
We see that
in enthuware.ocajp.i.v7.2.1276, the catch clause is using a method which is throwing a checked exception.
in enthuware.ocajp.i.v7.2.1006 the finally clause is using a method which is throwing a checked exception.

in enthuware.ocajp.i.v7.2.1276, we said: "It will not compile because of unhandled exception", so if the catch clause is using a method which is throwing a checked exception, this exception should be handled.

Why is it that we don't need to handle the checked exception thrown by the finally clause in enthuware.ocajp.i.v7.2.1006??

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Thu Jun 26, 2014 5:57 pm
by admin
Because there is a throws clause in main.

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Thu Aug 07, 2014 10:49 pm
by Marthinus
If I copy and paste the code in the question and I run javac on it, it compiles?

class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest{
public static void main(String [] args) throws Exception{
try{
m2();
}
finally{ m3(); }
}
public static void m2() throws NewException{ throw new NewException(); }
public static void m3() throws AnotherException{ throw new AnotherException(); }
}

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Thu Aug 07, 2014 10:51 pm
by Marthinus
Nevermind that. That is what is expected :|

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Sun Dec 28, 2014 9:54 pm
by chandu1987
Paul, I read somewhere that if a code inside a try block is throwing a checked exception, then the try block should always be followed by catch block. Can you clarify on this please?

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Sun Dec 28, 2014 11:31 pm
by admin
No, that is not correct. It may just have a finally or it may have a catch block that doesn't catch that exception and the method has a throws clause. Please try writing some test code to try out the possibilities.

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Mon Dec 29, 2014 8:35 am
by chandu1987
admin wrote:No, that is not correct. It may just have a finally or it may have a catch block that doesn't catch that exception and the method has a throws clause. Please try writing some test code to try out the possibilities.
Ok got it, Thank you.

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Sun Nov 22, 2015 9:09 pm
by meghajor
hi
Why does it say in the note newException thrown in the try block is ignored because of AnotherException in finally?
I understand finally will be executed no matter what.Why it doesnt print both the Exceotions?.



"Since finally throw AnotherException (due to a call to m3() ), the NewException thrown in the try block ( due to call to m2() ) is ignored and AnotherException is thrown from the main method."

Re: About Question enthuware.ocajp.i.v7.2.1006

Posted: Sun Nov 22, 2015 9:25 pm
by admin
Because that is how the language is designed. The caller of the method will not get NewException. It will get AnotherException. An exception thrown by code within finally block basically overrides the exception thrown by code in the try block.