Skip to content
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

runCatching catches Throwable? #22

Closed
lawkai opened this issue Jun 18, 2020 · 1 comment
Closed

runCatching catches Throwable? #22

lawkai opened this issue Jun 18, 2020 · 1 comment

Comments

@lawkai
Copy link

lawkai commented Jun 18, 2020

Hi, in Java, it is generally consider a bad practice to catch Throwable instead of Exception due to Throwable being the super class of Error which represents lower level errors like OutOfMemory etc.
May I know is there any reason why runCatching is catching Throwable instead of Exception?

@michaelbull
Copy link
Owner

michaelbull commented Jun 18, 2020

Because that's what Kotlin's own Result type (which came after this library and is far more limited than this library) does.

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catching.html#runcatching

Calls the specified function block and returns its encapsulated result if invocation was successful, catching any Throwable exception that was thrown from the block function execution and encapsulating it as a failure.

When this library was initially written I had an analogous method named Result.of which used Exception instead of Throwable, but then I deprecated it in favour of a top-level runCatching function to be consistent with how the stdlib operates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants