aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreported exception  Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreported exception  Problem" Watch "Unreported exception  Problem" New topic
Author

Unreported exception Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Getting error:
unreported exception java.lang.Exception; must be caught or declared to be thrown
new B().call();

on replacing the lines with these

a) public static void main(String arg[]) throws TestException
gives:

unreported exception java.lang.Exception; must be caught or declared to be thrown
new B().call();
b) ^
public static void main(String arg[]) throws Exception
gives:

unreachable statement
new A().call();}catch(TestException e){throw e;System.out.println("me e eemeemme");}

c) public void call() throws TestException
gives:

unreachable statement
new A().call();}catch(TestException e){throw e;System.out.println("me e eemeemme");}

please help


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
a) public static void main(String arg[]) throws TestException
gives:
unreported exception java.lang.Exception; must be caught or declared to be thrown
new B().call();


The method call() throws Exception which should be either caught in main() or thrown at higher level.
But, main doesn't catch it nor throws "Exception".
It throws TestException instead.

That's you get error unreported exception java.lang.Exception; must be caught or declared to be thrown


OCPJP6
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
b) ^
public static void main(String arg[]) throws Exception
gives:
unreachable statement
new A().call();}catch(TestException e){throw e;System.out.println("me e eemeemme");}


In this case, main() throws Exception, so there is no problems with throwing and catching Exceptions.
But the println() statement inside catch block in method call() is never reached.
This is because it rethrows the caught exception in previous line every time. So, the control flow goes outside the method - not reaching the println() statement.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
c) public void call() throws TestException
gives:
unreachable statement
new A().call();}catch(TestException e){throw e;System.out.println("me e eemeemme");}


The reason is same as for (b)
But in this case, the main() throws Exception which is broader than TestException thrown by method call()
 
wood burning stoves
 
subject: Unreported exception Problem