Spring @Async abstraction is fuzzy regarding exception handling

When using the Spring @Async annotation, abstraction occurs when it comes to the (flagged) exceptions in the throws method. The compiler will force the caller to handle the exception, but in fact the caller will never see the exceptions thrown by the @Async method. Instead, depending on the implementation, it will be processed and logged using Spring, or passed to the user-generated exception handler, or created when Future#get() called by the return value.

Therefore, I express the opinion that @Async methods should, as a rule, never throw checked exceptions. Instead, they should wrap all checked exceptions in the RuntimeException types RuntimeException that there is no throws .

Is this an accurate estimate? Is there any tool or software approach that fixes the leak? Does anyone know what Spring developers think, or if there are any plans to improve the situation? Thanks!

Perhaps related: Spring Async method hides exceptions

+5
source share
1 answer

Your rating is probably correct.

Now, if you want to handle the specified exception, simply do the following:

 @Async void mangle() { try { doMangle(); } catch (YourCheckedException e) { // Handle it } } void doMangle() throws YourCheckedException { ... } 
+2
source

Source: https://habr.com/ru/post/1211293/


All Articles