Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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()
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic