Handling Java Exceptions from Swift

50 views
Skip to first unread message

trei...@gmail.com

unread,
Nov 8, 2019, 5:43:57 PM11/8/19
to j2objc-discuss
Hi,

I'm currently building an iOS app in Swift binding to a shared Java library I transcode with j2objc.

Some of the Java code I'm translating with throws exceptions. These seem to be correctly translated into Objective-C. Eg,

this Java:

public void thrower() throws IllegalArgumentException {
throw new IllegalArgumentException("message");
}

results in:

- (void)thrower {

  @throw new_JavaLangIllegalArgumentException_initWithNSString_(@"message");

}

But it seems that NSExceptions aren't meant to be handled from Swift code and the convention to deal with this is to make ObjC functions return nil or boolean and set an optional NSError** argument as described here:

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html

I believe this is also what was suggested here:

https://groups.google.com/forum/#!searchin/j2objc-discuss/exception|sort:date/j2objc-discuss/dSbz2J13Z-U/TeH0lXUECQAJ

The following code seems like it would convert NSException to NSError to support try-catch blocks in Swift:

https://stackoverflow.com/a/36454808

How would I do the equivalent for Java exceptions? Just replace NSException with JavaLangException?

Is there a simpler way to handle this?

Thanks again!

trei...@gmail.com

unread,
Nov 12, 2019, 3:07:05 AM11/12/19
to j2objc-discuss
On Friday, November 8, 2019 at 2:43:57 PM UTC-8, trei...@gmail.com wrote:
The following code seems like it would convert NSException to NSError to support try-catch blocks in Swift:

https://stackoverflow.com/a/36454808

How would I do the equivalent for Java exceptions? Just replace NSException with JavaLangException?

I tried this and it seems to work as expected. 

Tom Ball

unread,
Nov 12, 2019, 5:54:08 AM11/12/19
to j2objc-discuss
Awesome! Would you like to be a guest blogger at https://j2objc.blogspot.com/ and describe this? It seems like it would be very useful to other developers in the future.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/e3093572-e0c3-453f-be39-e6c4b3659eba%40googlegroups.com.

trei...@gmail.com

unread,
Nov 15, 2019, 8:22:02 AM11/15/19
to j2objc-...@googlegroups.com
I would love to! Please see my direct mail for my first draft.

You received this message because you are subscribed to a topic in the Google Groups "j2objc-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/j2objc-discuss/UoieAIOujHo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/CAPLadK5ZDf9Rx32JSa%3Dwx6-NdL%2BJFWA%3Da42AFd3TGKzEyaX15g%40mail.gmail.com.

trei...@gmail.com

unread,
Nov 15, 2019, 8:22:02 AM11/15/19
to j2objc-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages