Just stumpled accross this and though this should ...
# stdlib
d
Just stumpled accross this and though this should be a feature: 1. Optional catch variable
try {
throw Error("Oh no!")
} catch {
console.log("Something bad happened, but don't know what")
}
2. Inline try catch without braces
var result = try throw Error("Oh no!") catch console.log("Something bad happened")
The question has been transfered to #compiler (https://kotlinlang.slack.com/archives/C7L3JB43G/p1655913770902479)
z
This is just code golf to avoid writing
(_: Throwable)
. And also a compiler thing rather than stdlib
d
Ignoring exceptions in most cases looks like a code smell anyway
☝️ 3
d
Ahh sorry there are somy channels and I thought stdlib is the most appropriate one, I will repost it in compiler
Many libraries use exeptions as result value, for example the jwt library for java throws an exception when the token is not valid. I have noticed this pattern in many libraries
d
Yeah, I understand But without specifying exception type you will catch (and ignore) everything, including smth like OutOfMemoryError And I don't think that it's what you ever want to do
👍 1
j
Copy code
val foo = runCatching { bar() }.getOrNull()
e
something like
catching
from https://youtrack.jetbrains.com/issue/KT-7128/Multi-catch-block may work out since it does limit the type of exception being caught, although it would be convenient if builder inference could infer the exception type without needing to specify it explicitly in the case of
.catching { handle(it) }