https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

bsimmons

04/12/2021, 1:40 PM
Is there an easy way to catch Java exceptions in multiplatform? I want to catch a specific
<http://javax.net|javax.net>.ssl.SSLPeerUnverifiedException
but I am only able to
catch(e: <http://io.ktor.utils.io|io.ktor.utils.io>.errors.IOException){...}
and nothing more specific than that. Any ideas?
m

mkrussel

04/12/2021, 1:43 PM
Create an expect Exception. Then typealias it for JVM and declare it for the other platforms. This is how Kotlin standard lib did it for IllegalArgumentException and IllegalStateException
👍 1
You might need to also declare the parent exceptions.
b

bsimmons

04/12/2021, 1:54 PM
Interesting. I'm not exactly sure how to setup the expect/actual. Something like this?
Copy code
expect class VpnException: Exception
actual class VpnException: javax.net.ssl.SSLPeerUnverifiedException()
The compiler doesn't seem to like that.
m

mkrussel

04/12/2021, 1:55 PM
public expect class IOException(message: String, cause: Throwable? = null) : Exception
public actual typealias IOException = IOException
That's what I did for IOException, make sure the actual part imports IOException to make it work correclty
b

bsimmons

04/12/2021, 1:58 PM
Hmmm, ok.
5 Views