Hi all! I have a question regarding `Exceptions in...
# multiplatform
j
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
It looks like your custom exception is getting caught, wrapped in another exception as its
cause
, and then thrown.
j
Exactly and I don't know if there is a way to make it works as we normally would use it
n
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
Thanks man! yes it was a proxy that was wrapping the exception. Really thanks for the great input 🙇‍♂️