• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tony Sitterly
Greenhorn
Posts: 3
Eclipse IDE Java jQuery
  • 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?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • 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
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Frits.
Your reply provided some needed clarity.
 
jalil idrissi
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic