Idiomatic way of catching multiple exceptions from Java in Swift

Đang mở
#545 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
java, swift
Lĩnh vực
developer-experience

Hướng nghiên cứu

Bắt đầu với ví dụ catch của File.swift và các wrapper FirstException và SecondException được tạo ra như trong issue. Tái hiện hành vi catch tổng quát bằng phương thức Java có thể ném cả hai exception, sau đó kiểm tra hành vi bridging exception giữa Swift và Java. Công việc được xem là hoàn tất khi cách xử lý phù hợp theo chuẩn được xác lập và ghi lại tài liệu, hoặc được triển khai bằng một ví dụ có thể tái hiện.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 🙂

Ngôn ngữ chính
Swift
Star
1.3k
Fork
127
Merge trung bình
2 ngày 14 giờ
Pull request đã merge (30 ngày)
5

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của swiftlang/swift-java

Tất cả issue của swiftlang/swift-java

Issue tương tự

Thêm issue về Swift

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.