> Markdown version of [Custom Error Handling](https://vaadin.com/docs/latest/flow/advanced/custom-error-handler). Section index: [llms.txt](https://vaadin.com/docs/latest/flow/llms.txt)

# Custom Error Handling

You can customize the error views related to navigation between different routes. The handling of router exceptions is described on the [Router Exception Handling](https://vaadin.com/docs/latest/flow/routing/exceptions.md) documentation page.

For other unhandled runtime exceptions, an `ErrorHandler` class can be used to let users know that something went wrong. To do so, first create a custom `ErrorHandler` class. Then use that class to override the default error handler.

For example, the following `CustomErrorHandler` class logs the error and displays a notification to the user:

```java
public class CustomErrorHandler implements ErrorHandler {

    private static final Logger logger = LoggerFactory.getLogger(CustomErrorHandler.class);

    @Override
    public void error(ErrorEvent errorEvent) {
        logger.error("Something wrong happened", errorEvent.getThrowable());
        if (UI.getCurrent() != null) {
            Notification.show("An internal error has occurred. " +
                    "Contact support for assistance.");
        }
    }
}
```

Next, assign the custom error handler to the current user session like this:

```java
VaadinSession.getCurrent().setErrorHandler(new CustomErrorHandler());
```

To apply the custom error handler to all user sessions, you can use a `SessionInitListener`, which receives an event each time a new `VaadinSession` is initialized. See the [Session and UI Listeners](https://vaadin.com/docs/latest/flow/advanced/session-and-ui-init-listener.md) documentation page to learn how to create a `SessionInitListener`.

`ErrorEvent` has the method `getComponent()` to get the handled `Component`, and the `getElement()` method to get the handled `Element` when the error is thrown, if available.

For example, the following button-click listener has `ErrorEvent` `getComponent()` return the clicked button:

```java
Button button = new Button("Click me", event -> {
    throw new IllegalArgumentException("No clicking");
});
```

## <a id="error-parameter-views-for-non-navigation-exceptions"></a>Error Parameter Views for Non-Navigation Exceptions

When an exception reaches the `DefaultErrorHandler`, it shows the `HasErrorParameter<T extends Exception>` error view registered for that exception type, if there is one. See the [Error Resolving](https://vaadin.com/docs/latest/flow/routing/exceptions.md) page for more information on the `HasErrorParameter`.

For a customized error handler, the same can be done by using the `ErrorHandlerUtil` method, `handleErrorByRedirectingToErrorView` like so:

```java
public class CustomErrorHandler implements ErrorHandler {

    private static final Logger logger = LoggerFactory.getLogger(CustomErrorHandler.class);

    @Override
    public void error(ErrorEvent errorEvent) {
        boolean redirected = ErrorHandlerUtil
            .handleErrorByRedirectingToErrorView(errorEvent.getThrowable());
        if (!redirected) {
            // We did not have a matching error view, logging and showing notification.
            logger.error("Something wrong happened", errorEvent.getThrowable());
            if (UI.getCurrent() != null) {
                Notification.show("An internal error has occurred. " +
                        "Contact support for assistance.");
            }
        }
    }
}
```

Calling `handleErrorByRedirectingToErrorView` shows the error view with the exact matching exception. As a result, for `AccountingException`, only the exact match `MyAccountingException implements HasErrorParameter<AccountingException>` is accepted and rendered.

`98746DB3-C9AC-42AD-A43D-D79F09B2155E`
