Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Overriden exception

 
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
 
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?)
 
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??
 
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.
 
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 ]
 
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.
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic