Skip to content

Instantly share code, notes, and snippets.

@haranicle
Created July 25, 2015 06:13
Show Gist options
  • Save haranicle/6b8679a6e06ba513fa74 to your computer and use it in GitHub Desktop.
Save haranicle/6b8679a6e06ba513fa74 to your computer and use it in GitHub Desktop.
do-catchについて #cswift #CodePiece
// エラーハンドリング専用構文
do {
// エラーが発生する可能性があるコード
} catch {
// エラー発生時の処理
}
// 例
do {
var handle = try File.open(path)
} catch {
// errorはErrorType型の変数
print(error)
}
do {
} catch FileOperationError.NotPermitted(let reason) {
} catch let error = PiyoError {
} catch is HogeError{
} catch {
}
var handle = try! File.open(path)
// とかくとdo-catchいらず。エラると強制終了。
// 呼び出し元にエラーの一部を委ねることもできる
func execute() throws -> String {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment