Last active
August 29, 2015 14:22
-
-
Save joanmolinas/a8cc3a16a2c3be664ac7 to your computer and use it in GitHub Desktop.
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
//New Feature on Swift2 doCatch-guard-markup | |
//: # Swift2 Features - do-Catch | |
import Darwin | |
//: Do-Catch type errors | |
enum DoCatchError : ErrorType { | |
case DifferentValuesError | |
} | |
//: Declare 3 constants | |
//: * cols | |
//: * rows | |
//: * total -> should be cols * rows | |
let cols = 10 | |
let rows = 10 | |
let total = 100 | |
//: This function check if total is cols * rows, if don't launch a error. | |
func validateParameters() throws -> Bool{ | |
//New Feature again - guard | |
guard total == cols*rows else { | |
throw DoCatchError.DifferentValuesError | |
} | |
return true | |
} | |
//: ## Do-catch Example | |
do { | |
//launch try for the exception | |
try validateParameters() | |
print("OK buddy") | |
}catch DoCatchError.DifferentValuesError { | |
fatalError("\(cols) * \(rows) != \(total)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment