I need to handle multiple exceptions (in this case...
# detekt
l
I need to handle multiple exceptions (in this case, IllegalStateException and a custom exception class) in a catch block. As far as I can tell, Kotlin does not currently support multi catch, so I have to catch Exception and check the types. Detekt sees this and gives me a TooGenericExceptionCaught error. This would be fine if I could use multi catch to check for both types, but that’s not part of the language yet. Should I just disable the detekt error, or is there a good workaround?
m
You could catch the specific types in different catch blocks and then delegate to a function to handle them all.
l
I must have misunderstood the statement that multicatch isn’t supported. I thought that included multiple catch blocks, but it looks like that works. Someone should take away my programming license.
m
Yeah, multicatch is catching multiple exceptions in the same catch block. I think it was added to Java in 7.0.
e
https://youtrack.jetbrains.com/issue/KT-7128 multi-catch in Java results in a variable with a union type, which is not expressible anywhere else in the language. it seems Kotlin designers want to get that part right before implementing multi-catch
l
That makes sense. They've been working on union types for a while. It's good to see the first 'union type' in definitely not nullable.
124 Views