Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active August 29, 2015 14:22
Show Gist options
  • Save joanmolinas/a8cc3a16a2c3be664ac7 to your computer and use it in GitHub Desktop.
Save joanmolinas/a8cc3a16a2c3be664ac7 to your computer and use it in GitHub Desktop.
//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