-
-
Notifications
You must be signed in to change notification settings - Fork 475
feat(core): Add ExceptionUtils.handleFatal to rethrow non-recoverable throwables #5907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,24 @@ public static boolean isIgnored( | |
| final @NotNull Throwable throwable) { | ||
| return ignoredExceptionsForType.contains(throwable.getClass()); | ||
| } | ||
|
|
||
| /** | ||
| * Handles non-recoverable {@link Throwable}s that should never be swallowed. Rethrows {@link | ||
| * VirtualMachineError} (e.g. OutOfMemoryError/StackOverflowError) and {@link ThreadDeath} as-is. | ||
| * For {@link InterruptedException}, restores the thread's interrupted status instead of | ||
| * rethrowing, since it is a checked exception. All other throwables are left untouched for the | ||
| * caller to handle/log/ignore as before. | ||
| * | ||
| * @param throwable - the throwable to check | ||
| */ | ||
| public static void handleFatal(final @NotNull Throwable throwable) { | ||
| // VirtualMachineError covers OutOfMemoryError, StackOverflowError, InternalError, and | ||
| // UnknownError | ||
| if (throwable instanceof VirtualMachineError || throwable instanceof ThreadDeath) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Agree that we should omit re-throwing LinkageError b/c of the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a valid point but I think we should re-throw |
||
| throw (Error) throwable; | ||
| } | ||
| if (throwable instanceof InterruptedException) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was a bit torn apart if this utility method should take care of interrupts as well, and decided to do it, as it will keep all call sites more tight. Along with that change the method is now a generic
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable to me, and definitely nice to have a common facility for restoring the interrupt flag. The only potential concern I can think of are methods that declare The alternative would be to have separate methods similar to what Project Reactor does:
But I prefer the current all-in-one approach more 👍
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's some good input! We could improve the ergonomics slightly by introducing method chaining. E.g. Let me task the clanker to have a look at this and the actual impact on our codebase
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it depends on the codebase and if we have use cases where we would need to do one and not the other. Can you ask the clanker to give you a report if we have such cases? |
||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: Thoughts about
rethrowIfFatal()?To me, that'd make its purpose more obvious at the call site (and I'm fine with its cheating a bit w/r/t restoring the interrupted flag).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah sounds good to me too, let's cheat a little 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with
rethrowIfFatal!