Reporting handled exceptions

In order to quickly understand and fix errors, it is often helpful to send exceptions that have been handled by your application to BugSnag, with additional diagnostic data that is specific to the error.

Basic usage

If you would like to send handled exceptions to BugSnag, you can pass any Throwable object to the bugsnag.notify method:

try {
    // Some potentially crashy code
} catch (Exception exception) {
    bugsnag.notify(exception);
}

This will create and send an event for a “handled” error, with the default severity “warning”.

Customizing diagnostic data

You can send handled exceptions with diagnostic data or other customizations by passing an OnErrorCallback as an argument to notify.

The callback receives a BugsnagEvent object as a parameter which can be used to add or amend the data sent to your BugSnag dashboard. You can also return false from the callback to prevent the event being sent at all:

try {
    // Some potentially crashy code
} catch (Exception exception) {
    bugsnag.notify(exception, (event) -> {
        // Add extra information
        event.addMetadata("account", "name", "Acme Co.");
        event.addMetadata("account", "paying_customer", true);

        // Increase severity
        event.setSeverity(Severity.ERROR);

        // Return `false` if you'd like to stop this error being reported
        return true;
    });
}

Or without using lambda syntax:

try {
    // Some potentially crashy code
} catch (Exception exception) {
    bugsnag.notify(exception, new OnErrorCallback() {
        @Override
        public boolean onError(BugsnagEvent event) {
            event.addMetadata("account", "name", "Acme Co.");
            event.addMetadata("account", "paying_customer", true);
            event.setSeverity(Severity.ERROR);
            return true;
        }
    });
}

See Customizing error reports for more information about the properties and methods available on a BugsnagEvent object.

Setting the severity

You can set the severity of an error in BugSnag by including the severity parameter when notifying BugSnag of the error:

import com.bugsnag.Severity;

bugsnag.notify(new RuntimeException("Non-fatal"), Severity.ERROR);

Valid severities are Severity.ERROR, Severity.WARNING and Severity.INFO.

Severity is displayed in the dashboard and can be used to filter the error list. By default all crashes (or unhandled exceptions) are set to ERROR and all bugsnag.notify calls default to WARNING.

Combining severity and metadata

You can combine severity and callback parameters:

bugsnag.notify(exception, Severity.INFO, (event) -> {
    event.addMetadata("diagnostics", "action", "user-initiated");
    return true;
});

Building a custom event

For more complex use cases, you can build a BugsnagEvent object and customize it before sending:

BugsnagEvent event = bugsnag.buildReport(exception);
event.setSeverity(Severity.WARNING);
event.setContext("PaymentProcessing");
event.addMetadata("transaction", "amount", 99.99);
event.addMetadata("transaction", "currency", "USD");
bugsnag.notify(event);

Logback integration

If you are using logback you can configure a BugSnag log appender to automatically report logged exceptions. See configuring logback for setup instructions.

With the logback appender configured, exceptions will be sent to BugSnag when you include the Throwable parameter in a logger call:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

try {
    // Some potentially crashy code
} catch (Exception exception) {
    logger.warn("Something went wrong", exception);
}