Hi Swifters,

I am wondering if it is possible to specify error types thrown in a
protocol method. By allowing us to do it and letting the compiler check all
the implementations of the protocol to make sure only they would only throw
the specified error types, we only need to catch our error types when
calling these methods.

For the code below:

enum MyError: Error {

    case justError

}

protocol MethodWillThrow {

    func testMethod() throws MyError

}


extension MethodThrow {

    func testMethod() throws {

        throw MyError.justError

    }

}

class TestClass: MethodThrow {

    func testMethod() throws {

        throw MyError.justError

    }

    func anotherMethod() {

        do {

            try testMethod()

        } catch MyError.justError {

            print("my error")

        } *catch {*

*            print("other error")*

*        }*

    }

}

Now we need add this extra default catch to make it compile and work and I
really want to remove this catch.

Please let me know if there is a way to do it and thanks for help in
advance.

Tim Wang
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to