kandi background
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Boilerplate open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Boilerplate

create-vite@2.9.2

4.5.0

v7.10.9

vite

create-vite@2.9.2

nuxt.js

ant-design-pro

electron-react-boilerplate

4.5.0

ignite

v7.10.9

Popular Libraries in Boilerplate

Trending New libraries in Boilerplate

Top Authors in Boilerplate

1

23 Libraries

640

2

19 Libraries

747

3

15 Libraries

31646

4

14 Libraries

178

5

14 Libraries

2987

6

13 Libraries

563

7

10 Libraries

3281

8

10 Libraries

69

9

9 Libraries

1759

10

9 Libraries

134

1

23 Libraries

640

2

19 Libraries

747

3

15 Libraries

31646

4

14 Libraries

178

5

14 Libraries

2987

6

13 Libraries

563

7

10 Libraries

3281

8

10 Libraries

69

9

9 Libraries

1759

10

9 Libraries

134

Trending Kits in Boilerplate

No Trending Kits are available at this moment for Boilerplate

Trending Discussions on Boilerplate

    Is Kotlin's runCatching..also equivalent to try..finally?
    How do I resolve error message: "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"
    ESlint - Error: Must use import to load ES Module
    Why are asynchronous runtimes like Tokio necessary?
    Best practice when dealing with C++ iostreams
    .NET 6.0 C# "new console template" - how to read CLI arguments?
    Can I use DerivingVia to derive instances for data types isomorphic to tuples
    Jetpack compose check if a function has been run in preview mode
    AVPlayer AVPlayerWaitingWhileEvaluatingBufferingRateReason slow loading for larger videos
    How to apply one signature test to multiple positionals

QUESTION

Is Kotlin's runCatching..also equivalent to try..finally?

Asked 2022-Mar-25 at 11:32

I want to run cleanup code after a certain block of code completes, regardless of exceptions. This is not a closeable resource and I cannot use try-with-resources (or Kotlin's use). In Java, I could do the following:

1try {
2  // ... Run some code
3} catch(Exception ex) {
4  // ... Handle exception 
5} finally {
6  // ... Cleanup code
7}
8

Is the following Kotlin code equivalent?

1try {
2  // ... Run some code
3} catch(Exception ex) {
4  // ... Handle exception 
5} finally {
6  // ... Cleanup code
7}
8runCatching {
9  // ... Run some code
10}.also {
11  // ... Cleanup code
12}.onFailure {
13  // ... Handle exception
14}
15

Edit: added boilerplate exception handling - my concern is with ensuring the cleanup code runs, and maintainability.

ANSWER

Answered 2021-Oct-28 at 14:24

As per Kotlin's doc for 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.

Even if finally always runs after a try block and also always runs after a runCatching, they do not serve the same purpose.

finally doesn't receive any argument and cannot operate on the values of the try block, while also receives the Result of the runCatching block.

TLDR; .runCatching{}.also{} is a more advanced try{}finally{}

Source https://stackoverflow.com/questions/69755646

Community Discussions contain sources that include Stack Exchange Network

    Is Kotlin's runCatching..also equivalent to try..finally?
    How do I resolve error message: "Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option"
    ESlint - Error: Must use import to load ES Module
    Why are asynchronous runtimes like Tokio necessary?
    Best practice when dealing with C++ iostreams
    .NET 6.0 C# "new console template" - how to read CLI arguments?
    Can I use DerivingVia to derive instances for data types isomorphic to tuples
    Jetpack compose check if a function has been run in preview mode
    AVPlayer AVPlayerWaitingWhileEvaluatingBufferingRateReason slow loading for larger videos
    How to apply one signature test to multiple positionals

QUESTION

Is Kotlin's runCatching..also equivalent to try..finally?

Asked 2022-Mar-25 at 11:32

I want to run cleanup code after a certain block of code completes, regardless of exceptions. This is not a closeable resource and I cannot use try-with-resources (or Kotlin's use). In Java, I could do the following:

1try {
2  // ... Run some code
3} catch(Exception ex) {
4  // ... Handle exception 
5} finally {
6  // ... Cleanup code
7}
8

Is the following Kotlin code equivalent?

1try {
2  // ... Run some code
3} catch(Exception ex) {
4  // ... Handle exception 
5} finally {
6  // ... Cleanup code
7}
8runCatching {
9  // ... Run some code
10}.also {
11  // ... Cleanup code
12}.onFailure {
13  // ... Handle exception
14}
15

Edit: added boilerplate exception handling - my concern is with ensuring the cleanup code runs, and maintainability.

ANSWER

Answered 2021-Oct-28 at 14:24

As per Kotlin's doc for 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.

Even if finally always runs after a try block and also always runs after a runCatching, they do not serve the same purpose.

finally doesn't receive any argument and cannot operate on the values of the try block, while also receives the Result of the runCatching block.

TLDR; .runCatching{}.also{} is a more advanced try{}finally{}

Source https://stackoverflow.com/questions/69755646

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Boilerplate

Tutorials and Learning Resources are not available at this moment for Boilerplate

Share this Page

share link

Get latest updates on Boilerplate

  • © 2022 Open Weaver Inc.