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

juancho

02/26/2019, 3:42 AM
Hi all! I have a question regarding
Exceptions in Multiplatform
(for the JVM target). I have a multiplatform lib with a method that can throw a custom exception. In Java code I’m using that method surrounded with a try/catch. I was expecting that custom exception to arrive but it was not working. I just added another catch with a general
Throwable
exception, now it worked and also what got my attention is that it’s a
RuntimeException
where the
cause
has my CustomException, so I can access it from there but it’s kind of annoying. Does anyone know how to improve this? So it looks like this:
Copy code
try {
            return mppApi.getSomethingWithExceptionInMultiplatform();
        } catch (MyCustomException e) {
            // never gets here...
        } catch (Throwable e) {
            if (e.getCause() instanceof MyCustomException) {
                // here I can access my custom exception fields with
                // ((MyCustomException) e.getCause())
            }
            throw e;
        }
j

josephivie

02/26/2019, 4:00 AM
It looks like your custom exception is getting caught, wrapped in another exception as its
cause
, and then thrown.
j

juancho

02/26/2019, 4:10 AM
Exactly and I don't know if there is a way to make it works as we normally would use it
n

natpryce

02/26/2019, 8:55 AM
The multiplatform-ness of the project shouldn’t, itself, cause the the exception to be wrapped. E.g. I’ve ported a Gradle build from JVM only to multiplatform on JVM, JS and native, and did not encounter any changes to the way exceptions are caught or thrown.
So maybe something else is wrapping it — a DI container or compiler plugin perhaps
j

juancho

02/26/2019, 4:35 PM
Thanks man! yes it was a proxy that was wrapping the exception. Really thanks for the great input 🙇‍♂️
6 Views