Using the EventFiringWebDriver. You can attach WebDriverEventListener to it and override methodes, ie the onException method:
EventFiringWebDriver driver = new EventFiringWebDriver(new FirefoxDriver());
WebDriverEventListener listener = new AbstractWebDriverEventListener() {
@Override
public void onException(Throwable t, WebDriver driver) {
// Take action
}
};
driver.register(listener);