Given the following code:
class MyException extends Exception{}
class TestExc6 {
public void m1() throws MyException
{throw new MyException();}
public void m2() throws RuntimeException
{throw new NullPointerException();}
public void aMethod() {
try
{
m1();
}
catch (MyException e)
{
m1();
}
finally
{
throw new RuntimeException(); //case (1)
//m2(); //case (2)
}
}
public static void main(
String[] args) {
TestExc6 tc = new TestExc6();
tc.aMethod();
}
}
Now when I compile this code with case (1) it compiles, and its OK because the compiler notices that an exception is thrown in the finally clause, so
the exception rethrown in the catch clause is dropped so there is no need
for a throws clause in the header of the method...
as in public void aMethod() throws MyException, Right ?
Now if I comment case(1) in finally and add case(2) a call to m2() which throws also an unckecked exception I get a compile error saying:
"unreported exception MyException; must be caught or declared to be thrown
m1();"
Can someone explain me why, aren't the two caes basically the same
Thanx