Skip to content

Instantly share code, notes, and snippets.

@traviskirton
Created July 24, 2015 19:54
Show Gist options
  • Save traviskirton/06041590e8af4d939267 to your computer and use it in GitHub Desktop.
Save traviskirton/06041590e8af4d939267 to your computer and use it in GitHub Desktop.
Getting used to the do try catch system in Swift2.0
//
// ViewController.swift
// test
//
// Created by travis on 2015-07-24.
// Copyright © 2015 C4. All rights reserved.
//
import Cocoa
enum TestError: ErrorType {
case trueError
}
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try willOnlyThrowIfTrue(true)
print("worked")
} catch TestError.trueError {
print("Caught true error")
} catch {
print("Another Error")
}
do {
try willOnlyThrowIfTrue(false)
print("worked")
} catch TestError.trueError {
print("Caught true error")
} catch {
print("Another Error")
}
}
func willOnlyThrowIfTrue(value: Bool) throws {
if value { throw TestError.trueError }
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment