[swift-users] Is it possible to specify error type thrown in a protocol method
Tim Wang
shenghai.wang at bigtincan.com
Wed Jul 5 19:37:54 CDT 2017
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170706/f40b6e09/attachment.html>
More information about the swift-users
mailing list