Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
Is Kotlin's runCatching..also equivalent to try..finally?

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:

try {
  // ... Run some code
} catch(Exception ex) {
  // ... Handle exception 
} finally {
  // ... Cleanup code
}

Is the following Kotlin code equivalent?

runCatching {
  // ... Run some code
}.also {
  // ... Cleanup code
}.onFailure {
  // ... Handle exception
}

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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

There is one important difference, where the code inside runCatching contains an early return. A finally block will be executed even after a return, whereas also has no such magic.

This code, when run, will print nothing:

fun test1()
    runCatching {
        return
    }.also {
        println("test1")
    }
}

This code, when run, will print "test2":

fun test2() {
    try {
        return
    } finally {
        println("test2")
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

There is one big difference between both code samples. try...finally propagates exceptions while runCatching().also() catches/consumes them. To make it similar you would have to throw the result at the end:

runCatching {
  // ... Run some code
}.also {
  // ... Cleanup code
}.getOrThrow()

But still, it is not really 1:1 equivalent. It catches all exceptions just to rethrow them. For this reason, it is probably less performant than simple try...finally.

Also, I think this is less clear for the reader. try...finally is a standard way of dealing with exceptions. By using runCatching() just to immediately rethrow you actually confuses whoever will read this code later.

Your question sounded a little like you believed Kotlin does not have try...finally and you need to search for alternatives. If this is the case, then of course Kotlin has try...finally and I think you should use it instead of runCatching().

over 4 years ago · Santiago Trujillo Relatório

0

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{}

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda