Skip to content

Instantly share code, notes, and snippets.

@thepatrick
Created August 9, 2022 07:44
Show Gist options
  • Save thepatrick/9e2bf326e92b4a4ced680e3316d04ee4 to your computer and use it in GitHub Desktop.
Save thepatrick/9e2bf326e92b4a4ced680e3316d04ee4 to your computer and use it in GitHub Desktop.
import Cocoa
public enum TagErrors: LocalizedError, CaseIterable {
case InvalidCallingConvention
var localizedDescription: String {
switch self {
case .InvalidCallingConvention: return "A closure was called with an invalid calling convention, probably (nil, nil)"
}
}
}
func test() throws -> String {
// throw TagErrors.InvalidCallingConvention
throw NSError(domain: "test", code: 0)
}
func otherThrows() throws {
do {
_ = try test()
} catch let error as TagErrors {
print("Got a TagErrror \(error.localizedDescription)")
}
}
func otherExhaustive() {
do {
_ = try test()
} catch let error as TagErrors {
print("Got a TagErrror \(error.localizedDescription)")
} catch {
print("Some other error that I will ignore")
}
}
func otherWithoutThrowAndNotExhaustive() {
do {
_ = try test() // Compiler Error: Errors thrown from here are not handled because the enclosing catch is not exhaustive
} catch let error as TagErrors {
print("Got a TagErrror \(error.localizedDescription)")
}
}
do {
try otherThrows()
} catch let localError {
print("other() threw: \(localError)")
}
otherExhaustive()
otherWithoutThrowAndNotExhaustive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment