• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Clarification

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm probably overusing this forum tonight...but this should be my last question....for awhile.
Please let me know if this is correct:
If I declare that a method throws a particular error. Then I can throw that error in the method and/or I can throw any error that is more general (superclasses) to that exception.
Regarding overriding a method that throws an exception. I can throw that exception or one that is more specific (subclasses) to that exception....or I can provide the overriding method without having it throw any exception.
Am I on the right track here?
- Mike
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Cunningham:
I'm probably overusing this forum tonight...but this should be my last question....for awhile.
Please let me know if this is correct:
If I declare that a method throws a particular error. Then I can throw that error in the method and/or I can throw any error that is more general (superclasses) to that exception.


Actually you have that backwards. You can throw subclasses of any Exceptions you declare that your method throws, but you cannot throw superclasses, because while this statement is true due to OO-Inheritance: a subclass IS a superclass, the contrary is not.


Regarding overriding a method that throws an exception. I can throw that exception or one that is more specific (subclasses) to that exception....or I can provide the overriding method without having it throw any exception.
Am I on the right track here?
- Mike


Yes to both. You can't throw an exception that is more general than what you declared. So you can throw the exception declared in the superclass, or subclasses of that exception. You can also choose to not throw anything.
 
Mike Cunningham
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, thank you for the clarification.
reply
    Bookmark Topic Watch Topic
  • New Topic