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?
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?
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.