Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

init() and ServletException

 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a chance of init() method throwing ServletException while executing, then why we are not declaring in the throws clause.
But the same exception is specified in the throws clause of service() method.Is it not a checked exception ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you define what init does, you know whether you're going to throw a ServletException or not. If you do, add the throws clause. Simple.
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear,I couldnot get your exact intention in the reply.
I request you to please make a note of these lines :
1) All I know is, a method must specify a checked exception in throws clause else it must handle it by putting try.. catch block
So I am assuming ServletException is a checked one.If this is true, then why the same thing is not happeninh with init() ?
2) Also how to know whether an exception is checked or not.I am not able to find it in the API documentation

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you are confused. if your init() method throws ServletException, it must be declared in a throws clause just like any other checked exception.
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear, now I am able to draw something.Please check this.
1)If any code that we write in the init() tends throw a ServletException then only we must specify it in the throws clause or else put a try catch block to handle it, otherwise the program will not compile.So only in cases where the code might throw a SE then we should follow the above procedure.
Am I following the right way ?
So if there is some code in init()which throws SE, then why API apecification doesnot mandated to specify it (SE) in the throws clause of init() like service() specifying the same in it's throws clause.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijayk gopu:

So if there is some code in init()which throws SE, then why API apecification doesnot mandated to specify it (SE) in the throws clause of init() like service() specifying the same in it's throws clause.


It does.

Copied-and-pasted from the Javadoc:

init

public void init()
throws ServletException
A convenience method which can be overridden so that there's no need to call super.init(config).
Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().


Throws:
ServletException - if an exception occurs that interrupts the servlet's normal operation
 
vijay kumarg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear, thankyou very much.I am learning alot here with the help of people like you.The doubt is arised because in a typical servlet class I have not saw init() throwing ServletException nor a try catch handling it.But the javadoc
is showing SE in throws clause
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Docs and Specs. are must. There is no other better substitute.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!