Idiomatic way of catching multiple exceptions from Java in Swift

オープン
#545 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
java, swift

調査の方向性

File.swift の catch 例と、issue に示されている生成された FirstException および SecondException のラッパーから始めます。両方の例外をスローできる Java メソッドを使って汎用 catch の動作を再現し、その後、Swift と Java の例外ブリッジングの動作を調査します。サポートされる慣用的な処理が確立されて文書化されるか、再現可能な例で実装されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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 🙂

主要言語
Swift
スター
1.3k
フォーク
127
平均マージ
2日 14時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

swiftlang/swift-java のほかの issue

swiftlang/swift-java の issue をすべて見る

似ている issue

Swift の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。