aspose file tools*
The moose likes Java in General and the fly likes EXCEPTION DILEMA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

EXCEPTION DILEMA

Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
I posted this on the SJCP but I got no answer so far so I'll try my luck here also...
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


"Did anyone understand what I have just explained? ... because I did not!"
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Original thread, to which any further replies should be directed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EXCEPTION DILEMA
 
Similar Threads
compilation error in try/catch/finally
Too many exceptions?
Exception
exception dilema
Exceptions raised in catch block code