---
title: Customize the Java SDK error handler
slug: feature-experimentation/docs/customize-error-handler-java
docTags: 
createdAt: 2026-08-05T20:39:53.000Z
---

You can provide your own custom **error handler** logic to standardize across your production environment.

This error handler is called for SDK errors such as an unknown experiment or event key, an invalid datafile, or a user profile service failure. An unknown feature flag key does not call the error handler; the decide methods return an error decision instead.

Here is a code example where an error handler from the SDK is used. If the error handler is not overridden, a no-op error handler is used by default.

```java
import com.optimizely.ab.Optimizely;
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
import com.optimizely.ab.event.AsyncEventHandler;
import com.optimizely.ab.event.EventHandler;

EventHandler eventHandler = new AsyncEventHandler(20000, 1);

// Default handler that raises exceptions
ErrorHandler errorHandler = new RaiseExceptionErrorHandler();

// Create an Optimizely client with the default event dispatcher
Optimizely optimizelyClient = Optimizely.builder(datafile, eventHandler)
                                  .withErrorHandler(errorHandler)
                                  .build();
```

For additional control and visibility into the errors coming from the Optimizely Feature Experimentation Java SDK, you should implementing your own custom error handler. With a custom error handler, you can choose what to do with an error, whether it may be as simple as logging the error to console or sending the error to another error monitoring service. The following is an example of using a custom error handler to log errors:

```java
import com.optimizely.ab.Optimizely;
import com.optimizely.ab.error.ErrorHandler;
import com.optimizely.ab.OptimizelyRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Custom handler that logs the errors
public class CustomErrorHandler implements ErrorHandler {
  	private static final Logger logger = LoggerFactory.getLogger(CustomErrorHandler.class);
  
    @Override
    public <T extends OptimizelyRuntimeException> void handleError(T exception) {
        logger.error(exception.getMessage());
    }
}

EventHandler eventHandler = new AsyncEventHandler(20000, 1);
ErrorHandler customErrorHandler = new CustomErrorHandler();

// Create an Optimizely client with the default event dispatcher
Optimizely optimizelyClient = Optimizely.builder(datafile, eventHandler)
                                  .withErrorHandler(customErrorHandler)
                                  .build();
```
