Forums Register Login

Doubt regarding init() block execution

+Pie Number of slices to send: Send
What will happen if some sort of exception occurs inside the init() block? Will the container still be able to service client requests? Or do we have to rectify the problem before trying to use the servlet instance?
+Pie Number of slices to send: Send
What sort of exception are you talking about ? I mean as far as I know , the init() method does not declare any checked exception and all exceptions have to be programmticaly caught inside the method.
+Pie Number of slices to send: Send
Saif, I was asked this question in a job interview. The guy asked me that what will happen if an exception occurs inside the init() block? He did not mention which exception. Will you still be able to service the requests made from the browser? I replied in negative. Then he asked me why? I could not answer that question. Any idea?
+Pie Number of slices to send: Send
If the servlet init() block throws an excpetion , then it wont be able to service any requests , that is certain .
+Pie Number of slices to send: Send
If the servlet init() block throws an excpetion , then it wont be able to service any requests , that is certain . Now regarding the reason , from docs we can see

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

1- Throws a ServletException
2- Does not return within a time period defined by the Web server



So , logicaly if ServletException is thrown from init() block , the servlet was never put into service by the container and thus its unable to service any requests.
+Pie Number of slices to send: Send
Another thing , Ignore my message about the init method not throwing anything ( I couldn't edit my post ) . The init method does throw a ServletException. From javadocs

Throws:
ServletException - if an exception has occurred that interferes with the servlet's normal operation

 
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 787 times.
Similar Threads
Question on static
Init Blocks?
About Method local inner class
Exam Lab Question
Block (not static)in a class...??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:09:51.