How to catch multiple exceptions simultaneously in Kotlin?
Hello everyone! I started learning Kotlin and encountered a problem with exception handling. In Java, I'm used to writing `catch (IOException | SQLException e)` to catch multiple exception types in one block. But in Kotlin, this syntax doesn't work.
I need to handle situations where parsing a number from a string can throw `NumberFormatException`, and when accessing a file — `FileNotFoundException` and `IOException`. In Java, I would write:
```java
try {
// some code
} catch (IOException | NumberFormatException e) {
// handling
}
```
And in Kotlin, I tried this:
```kotlin
try {
// code
} catch (e: IOException | NumberFormatException) { // doesn't compile
// handling
}
```
But the compiler complains. Please advise how to properly catch multiple exceptions in Kotlin.
No answers yet.
Log in to leave an answer