나는 내 자신의 오류를 만들고 스위프트에서 throws
키워드로 트리거하는 방법을 알게되었습니다. 다른 언어 (또는 Ruby rescue
)에있는 일반 try - catch
을 처리 할 수없는 예외를 복제하는 방법은 얻을 수 없습니다. (스위프트에서)Swift에서 try-catch를 사용하는 방법?
예 :
func divideStuff(a: Int, by: Int) -> Int {
return a/by
}
let num = divideStuff(4, by: 0) //divide by 0 exception
여기
내가 예를 들어, C#으로 그것을 다룰 줄 방법은 다음과 같습니다
int DivideStuff(int a, int b) {
int result;
try {
result = a/b;
}
catch {
result = 0;
}
return result;
}
가 어떻게 스위프트와 같은을 달성 할 수있다?
그것의'do' ..'다큐먼트에서 catch' : https://developer.apple.com/ 라이브러리/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html – Jonas
try */catch와 함께 임의의 런타임 예외 (예 : 0으로 나누기)를 catch 할 수 * 없습니다. http://stackoverflow.com/questions/34628999/swift를 비교하십시오. -force-unwrapping-exception- 유사한 질문과 문서 링크로 전파되지 않습니다. –