• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Throwing exception from a ServletContextLister Implementation

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm trying to throw a 'ServletException' from a method, in a class which implements 'ServletContextListener' in eclipse. Here is my code which is failing.





Is it possible to throw an exception from this class? Please guide me.
Thanks

Aditya
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this section of the Java Language Specs. Specifically:


A method that overrides or hides another method (ยง8.4.8), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.



So you are trying to throw a checked exception (ServletException) from contextInitialized. But the contract for contextInitialized does not allow for a ServletException to be thrown (doesn't declare it in the method signature, see its API) so your implementation can't do throw the exception.

I am not sure you want to do that anyway. If you throw an exception, and the container doesn't expect it, what you should expect is that the entire application comes crashing down. Are you sure that is what you want? Why not report the failure and supply a default value?
 
Aditya Raghu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thats what I intended to do. I want to check for a string in a file when my application loads. If the string is different from the one that is required I want to stop the application from coming up in tomcat. I was asked to do this way i.e throw a ServletException from a method in a class, which implements ServletContextListener. Is there any way around this? I'm not able to compile the code this way.

 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can't throw a ServletException, or any checked exception. You will have to throw a java.lang.RuntimeException or one of its subclasses (try to pick one that is meaningful to your situation) or make a custom exception which subclasses RuntimeException. RuntimeExceptions are not checked exceptions, so you can throw them from methods which don't declare them in the method signature.
 
Aditya Raghu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Steve. Its working after I used Runtime Exception.


Aditya
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic