File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic


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() {
catch (MyException e)
throw new RuntimeException(); //case (1)
//m2(); //case (2)

public static void main(String[] args) {
TestExc6 tc = new TestExc6();
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
Can someone explain me why, aren't the two caes basically the same

"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:
jQuery in Action, 3rd edition