This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Error: ErrorType { | |
| case SomeExpectedError | |
| case SomeUnexpectedError | |
| } | |
| func functionThatThrows() throws { | |
| throw Error.SomeExpectedError | |
| } | |
| XCTAssertThrowsError(try functionThatThrows(), "some message") { (error) in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // disable error handling | |
| try! division(numerator: 10, denominator: 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum DivisionError: Error { | |
| case dividedByZero | |
| } | |
| func division(numerator: Int, denominator: Int) throws { | |
| if denominator == 0 { | |
| throw DivisionError.dividedByZero | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // create an enum with error values | |
| enum DivisionError: Error { | |
| case dividedByZero | |
| } | |
| // create a throwing function using throws keyword | |
| func division(numerator: Int, denominator: Int) throws { | |
| // throw error if divide by 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| do { | |
| try division(numerator: 10, denominator: 0) | |
| ... | |
| } | |
| catch DivisionError.dividedByZero { | |
| // statement | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // call throwing function using try keyword | |
| try division(numerator: 10, denominator: 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // create throwing function using throws keyword | |
| func division(numerator: Int, denominator: Int) throws { | |
| // throw error if divide by 0 | |
| if denominator == 0 { | |
| throw DivisionError.dividedByZero | |
| } | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LoginViewController: UIViewController { | |
| @IBOutlet weak var nameTextField: UITextField! | |
| @IBOutlet weak var passwordTextField: UITextField! | |
| override func viewDidLoad() { | |
| } | |
| func showError(_ message: String) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension ValidationError: LocalizedError { | |
| var errorDescription: String? { | |
| switch self { | |
| case .emptyName: | |
| return "Please enter name." | |
| case .shortName: | |
| return "Name is too short." | |
| case .longName: | |
| return "Name is too long." | |
| case .emptyPassword: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func makeRequest() { | |
| do { | |
| let name = try ValidateService.validate(name: nameTextField.text) | |
| let password = try ValidateService.validate(password: passwordTextField.text) | |
| self.sendLoginRequest(name: name, password: password) | |
| } catch { | |
| self.showAlert(error.localizedDescription) | |
| } | |
| } |
NewerOlder