I have a Spring Bean class(Test) which is registered as bean in applicationContext.xml file. This bean is loading during the application startup itself, it contains application initialization stuff. But while loading application one of the bean class method giving exception. I need to handle this error/exception case and need to show as 500 custom error.jsp page.

Sample code snippet :

```
class TestBean {
    public TestBean(){
        doStuff() ;
    }

    public void doStuff() {
    try {
        //some business logic
    } catch(Exception e) {
        logger.error("error message"); //here i am just logging the error message
    }
    }
 }

```

I have tried in different scenarios like below :

1) Throwing custom exception with 500 as the error code.

```
 class TestBean {
    public TestBean() throws CustomCheckedException{
        doStuff() ;
    }

    public void doStuff() throws CustomCheckedException{
    try {
        some logic
    } catch(Exception e) {
        throw new CustomCheckedException(500,"errorMessage");
    }
    }
 }

public class CustomCheckedException extends Exception {

    public static final Logger logger = Logger.getLogger(CustomCheckedException.class.getName());

    String errorMessage;

    int errorCode;

    public CustomCheckedException(int errorCode, String errorMessage) {
        super();
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;

    }

    public CustomCheckedException() {

    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

```

web.xml code :

```
<error-page>
    <error-code>500</error-code>
    <location>/error.jsp</location>
</error-page>

```

error.jsp page is located in webapp folder

2) Not throwing any custom exception just giving specific exception in web.xml like below.

```
    <error-page>
 <exception-type>java.util.concurrent.ExecutionException</exception-type>
        <location>/error.jsp</location>
    </error-page>

```

But i am not able to display the custom error.jsp page when the exception occured during application initialization.
I have seen some other way also like :
res.sendError(HttpServletResponse.SC\_INTERNAL\_SERVER\_ERROR, "error message");
but in my bean class i dont have the response object. If i try to Autowire HttpServletResponse into my bean class still i am getting response object as null only.

Can any one tell me where i am doing wrong and which approach is good for displaying custom error page if any exception occurs during application startup.