Idiomatic way of catching multiple exceptions from Java in Swift

Offen
#545 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
25/100
Issue-Typ
Feature
Klarheit
Muss geklärt werden
Aktivitätsstatus
Veraltet
Tech-Stack
java, swift

Rechercherichtung

Beginne mit dem File.swift-Catch-Beispiel und den im Issue gezeigten generierten FirstException- und SecondException-Wrappern. Reproduziere das Verhalten des generischen Catchens mit der Java-Methode, die beide Exceptions auslösen kann, und untersuche anschließend das Swift-Java-Exception-Bridging-Verhalten. Als erledigt gilt, wenn die unterstützte idiomatische Behandlung festgelegt und dokumentiert oder mit einem reproduzierbaren Beispiel implementiert ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Description

Hi everyone!

When I worked on integrating local Java library to my Swift package, I haven't found any mention about how to handle exceptions thrown by Java code in my Swift code.

I mean I still can use something like:

// File.swift

let myJavaClass = try JavaClass<MyJavaClass>(environment: jniEnvironment)

do {
   try myJavaClass.throwException()
} catch {
    print(error)
}

And I might have wrapped error in a string, and compare the results, but this is not what I really wanted to do...

The throwException() can throw multiple different exceptions, which are: FirstCustomException, SecondCustomException.
Their implementation in Java very simple:

// Java file

public class FirstException extends Exception {
    public FirstException(String message) {
        super(message);
    }
}

public class SecondException extends Exception {
    public SecondException(String message) {
        super(message);
    }
}

Since I added both to my swift-java config file, I can see them built by Swift-Java:

// Auto-generated by Java-to-Swift wrapper generator.
import CSwiftJavaJNI
import SwiftJava

@JavaClass("org.FirstException")
open class FirstException: Exception {
  @JavaMethod
  @_nonoverride public convenience init(_ arg0: String, environment: JNIEnvironment? = nil)
}

// And the same for another exception as well...

So, my naive approach was to use catch let syntax:

do {
   try myJavaClass.throwException()
} catch let error as FirstException {
  // do something with error
} catch let error as SecondException {
  // do something with error
}

And, it's built successfully, but neither of catch let blocks are triggered.
If I added generic catch block, it is the one that will be executed.

Question

So, my question is, what is the idiomatic way of handling errors in the scenarios similar to one I described.

Thank you in advance 🙂

Vorherrschende Sprache
Swift
Sterne
1.3k
Forks
127
Ø Merge
2 T. 14 Std.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus swiftlang/swift-java

Alle Issues in swiftlang/swift-java

Ähnliche Issues

Weitere Issues zu Swift

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.