This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriden exception

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why this code show error, i taught the overridding method can be sub-class of the exception of the overridden method?
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry there is one more class
 
Jack malvin
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is SmallFish?
Is she killed by some Exception.
Oops.. who is going to Handle the Exception thrown by test() ? (... More killing?)
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {
a.test();
} catch (Exception e) {
// clean up
}
 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
smallfish juz added....

 
ming ming
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do we want to use throws instead of throw, i still do not see any usage of throws..can anyone show me how throw can never take over the place of throws??
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throws comes with the method signature while throw new Exception() is a statement.
 
nitin pokhriyal
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

what i found in the code that your test() method of Swim class is using throws statement. So to compile it either you remove throws statement or put try-catch block in the main method of testFish class and use throw statement in test() method's catch block. If you want your caller method shd take care of exception then use throw statement in test method.

anyone Correct me if i m wrong..

Thx,
Nitin

[ June 03, 2006: Message edited by: nitin pokhriyal ]
[ June 03, 2006: Message edited by: nitin pokhriyal ]
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throws is a clause, while throw is a keyword. They are used with the Handle or Declare rule which states that:

If you do not want to catch an exception that is thrown in your method, then you can use the throws clause in the declaration of the method in which the exceptions takes place, to let the class where the method call occured handle the exception. So, the rule of thumb is : if you are catching the exception and not throwing it again then you dont need to use the throws clause.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic