Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exam watch question

 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
 
Hiruka Sandev
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.
 
S Bhanage
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic