同步异常抛出的基本使用 try catch throw throws

26. January 2016 Swift 0

enum Error:ErrorType{

case WrongJson

} //自定义异常类型

func test() throws{

do{

try NSJSONSerialization.JSONObjectWithData(NSData(), options: .AllowFragments)

}

catch{

throw Error.WrongJson

}

}

do{

try test()

}

catch Error.WrongJson{

print(“error”)

}


Leave a Reply

Your email address will not be published. Required fields are marked *