0

Handling exceptions, I found myself needing this handy little function :

inline fun exec(lambda: () -> Any): Boolean = try { lambda() ; true } catch(e:Exception) { false }

Does functions like that exist in Kotlin ? Is there idiomatic altenatives to the heavy try-catch-finnaly syntax ?

7
  • 2
    Out of curiosity: What is your use case for that? Personally I prefer exceptions over a function that just returns a successful boolean because different exceptions can be thrown in different error scenarios, allowing for different error handling. Commented Dec 11, 2020 at 15:25
  • This is a bad idea, for several reasons.  If an exception was thrown, how do you tell what went wrong?  Your function swallows the exception completely, without even logging it.  So there's no way to find out what exception was thrown, where it was thrown, or what message it contained.  (And if you're assuming that only one thing could possibly go wrong, you're very probably missing something…)  A function like this makes it impossible to debug coding problems, or to handle more than one type of error condition… Commented Dec 11, 2020 at 17:20
  • …It also needs (but doesn't force) the direct caller to check the return value, and either handle it there and then, or return it to its caller, and so on.  Finally, it requires the lambda to return a non-nullable values, and then ignores it. Commented Dec 11, 2020 at 17:20
  • There are certain situations when interacting with Java code where it might make sense to wrap Exception-throwing code with something that just returns null on failure. But usually, you would only want to wrap the specific exceptions you're expecting like IllegalArgumentException and NumberFormatException, as a way to swallow exceptions that you don't need to log because they indicate something that you expected might happen, like if you pass user input for some kind of validation. Commented Dec 11, 2020 at 18:49
  • @vatbub It was something like that kotlin val isAudio = isAudioFile(source) && exec { m.setDataSource(source.canonicalPath) } setDataSource would throw if there was any problem of data avaibility Commented Dec 11, 2020 at 19:18

1 Answer 1

1

You can use runCatching and then use .isSuccess on the result to get a Boolean for success

runCatching {
    // your code
}.isSuccess

If your code needs a result, and you want to return null on failure (which is idiomatic Kotlin) you can do

val foo: Foo? = runCatching {
    // your code that produces a Foo
}.getOrNull()
Sign up to request clarification or add additional context in comments.

1 Comment

runCatching can be a bit dangerous. If the example code above were to an OutOfMemoryError (or any other java.lang.Error that you probably shouldn't catch), it would get swallowed up. Consider using try/catch instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.