You are currently viewing Why do Kotlin file I/O exceptions not map to Java ones?

Why do Kotlin file I/O exceptions not map to Java ones?

Kotlin stdlib exceptions often map to their Java equivalents on the JVM platform.

kotlin.Exception::class.java.name == "java.lang.Exception" kotlin.RuntimeException::class.java.name == "java.lang.RuntimeException" kotlin.IllegalStateException::class.java.name == "java.lang.IllegalStateException" kotlin.NoSuchElementException::class.java.name == "java.util.NoSuchElementException" kotlin.IllegalArgumentException::class.java.name == "java.lang.IllegalArgumentException" kotlin.UnsupportedOperationException::class.java.name == "java.lang.UnsupportedOperationException" 

Including to exceptions in subpackages of java.nio.

kotlin.text.CharacterCodingException::class.java.name == "java.nio.charset.CharacterCodingException" 

But these don’t, despite there being same-named exceptions in java.nio.file.

kotlin.io.NoSuchFileException::class.java.name == "kotlin.io.NoSuchFileException" kotlin.io.FileAlreadyExistsException::class.java.name == "kotlin.io.FileAlreadyExistsException" 

Anyone know why? Just an oversight, or was this a deliberate choice? This bit me yesterday because some of the kotlin.io extension functions such as Path.deleteExisting() are declared as throwing these two exceptions, but they throw the Java ones, not the Kotlin ones.

submitted by /u/koreth
[link] [comments]