• 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

exam watch question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

This question is from K&B book in the exam watch topic.

The below code won't compile. Here I will get unreported exception MyException; must be caught or declared to be thrown.

I dont understand what actually the code is telling. As I know doStuff() declaring the checked exception which is thrown by us and this method is not handling that exception

Am I right? If I am wrong please explain me what is going on in the program?




Thanks in advance,
Jaya
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jayalakshmi charugundla wrote:
As I know doStuff() declaring the checked exception which is thrown by us and this method is not handling that exception



That is correct. 'someMethod' needs to explicitly handle the 'MyException' exception which is thrown by the 'doStuff' method.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Since MyException extends "EXCEPTION", it can be handled as:
1) Using try-catch
2) Declare in throws clause.

Your someMethod() should use either of the above step.
OR
You can extend "RuntimeException" and no need to change
any code further.

It compiles with this minor change.
Hope this is helpful to you.

 
My pie came with a little toothpic holding up this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic