• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

OCEJWCD study companion ch4 q13 (ServletContextAttributeListener methods - checked exceptions)

 
Greenhorn
Posts: 3
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand why the listener methods are not allowed to throw any checked exceptions?
I can't seem to find any reference for this in the chapter text?
 
Creator of Enthuware JWS+ V6
Posts: 3346
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

I am trying to understand why the listener methods are not allowed to throw any checked exceptions?


The obvious answer is because the interface doesn't allow this.

On the other hand if you think of it: why would you want to throw a checked exception? Could you catch it in your business logic and do something about it? Not really, don't forget that it is the web-container calling these methods because of an event in your business logic. So if you throw a checked exception what should the web-container do?

Regards,
Frits
 
Tony Sitterly
Greenhorn
Posts: 3
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Frits.
Your reply provided some needed clarity.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is actually in SCJP. When you override a method, you can only throw a narrower exception. It means in the signature you declare that you "throws" a subclass of the overriden method, or you don't declare anything at all.
 
A timing clock, fuse wire, high explosives and a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic