• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Unreported exception Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()
 
reply
    Bookmark Topic Watch Topic
  • New Topic