Sentry JAVA - Exception not handle by Sentry or loggers

Hello !

In JAVA, we have different exceptions. We can catch them with a try/catch. But what if we forget or we don’t want to put a try/catch block in the code, how to catch it with Sentry ? What if we don’t know that a part of the code have a risk to have an exception ?

It seems that sentry or sentry-spring don’t handle basic JAVA exception (or I don’t know how…) without a try/catch block.

Example :

We write this part of code and do the different configuration for having Sentry in our application :

public static void main(String args) {
double value = 1 / 0;
}

If you run this part of code, Sentry will not send an error but, we can see it on the Console.

Any help ? Thanks for your reply !

This should be captured via the UncaughtException handler.

Sentry’s SDK for Java will flush out the event before the process exists.

What version of Java are you using? And how are you running that Java program?

Hello !

Thanks for your reply !

I’m using JAVA 11. I’m running my program in a Tomcat.

I tried to enable the UncaughtExceptionHandler but, it doesn’t work. I add it in the init :

Sentry.init(options → {

options.setEnableUncaughtExceptionHandler(true);
});

So, if I understand what you said, Sentry could handle all errors by JAVA by using UncaughtException handler.

Another example that would help you to understand what I mean :

You have a RestController and you make a request on an endpoint. On this endpoint, you forget to put anything to catch the error, or don’t want to do that, or you don’t know that an error will be raised.

@RestController(value="/test")
public class TestRestController {
@GetMapping
public void get() {

// Here you have an error like java.lang.ArithmeticException, NPE or other

}
}

In regards to Spring MVC integration, we have SentryExceptionResolver that catches unhandled exceptions that are thrown during request processing. If you use Spring Boot, it gets autoconfigured and works out of the box. Take a look at docs

In regards to handling unhandled exceptions in a console application, it works for me as expected. Check sample: GitHub - maciej-scratches/sentry-console-unhandled: https://forum.sentry.io/t/sentry-java-exception-not-handle-by-sentry-or-loggers/14204/2

Hello !

Thanks for your reply.

Interesting thing ! I use Spring and not Spring Boot but it doesn’t work again. Maybe I have done some mistakes… I use this configuration :

Configuration
EnableSentry (
dsn = “…”,
exceptionResolverOrder = Ordered.LOWEST_PRECEDENCE
)
public class Config {

[…]

Bean
public void sentryInit() {
Sentry.init(options → {
options.setDsn("…");
// To set a uniform sample rate
options.setTracesSampleRate(1.0);
options.setEnableUncaughtExceptionHandler(true);
});
}
}

[…]
}

Note : With this configuration, I can send errors on Sentry with only loggers.
Note 2 : I remove the @ because it causes trouble to post this message

Take a look here for a working sample with plain Spring: sentry-java/sentry-samples/sentry-samples-spring at main · getsentry/sentry-java · GitHub

If it still doesn’t help, I can take a look at your code if you could post a fully working application on github that reproduces this issue.

Hello !

After investigated, it seems that my application do some mystical things when I have Java errors… So, I will investigate that in my side.

But, thanks a lot for your time to reply to me ! It works well on a classical application (and not mine).

Note: It seems the command to run the program at your link doesn’t work well. The task doesn’t exist.