do-catch

do try catch swift 2

随声附和 提交于 2019-12-08 07:46:48
问题 HI i am a little confused about how to transfer if_else error handling to do try catch successfully. Here is my code. let error : NSError? if(managedObjectContext!.save()) { NSNotificationCenter.defaultCenter().postNotificationName("updateUndoState", object: nil) if error != nil { print(error?.localizedDescription) } } else { print("abort") abort() } and now i converted to swift 2.0 like this do { try managedObjectContext!.save() } catch { NSNotificationCenter.defaultCenter()

Swift Error Handling For Methods That Do Not Throw [duplicate]

不想你离开。 提交于 2019-12-01 02:24:51
This question already has an answer here: How do I catch “Index out of range” in Swift? 4 answers How to handle errors for methods or code that does not explicitly throw? Wrapping it a do / catch block results in a compiler warning: "'catch' block is unreachable because no errors are thrown in 'do' block" Coming from C# / JAVA background this is an oddity to say the least. As a developer I should be able to safeguard and wrap any code block in do/catch block. Just because a method is not explicitly marked with "throw" does not mean that errors will not occur. Faced with an exception thrown

Swift专题讲解十八——异常处理

故事扮演 提交于 2019-12-01 01:23:22
Swift专题讲解十八——异常处理 一、异常的抛出与传递 代码的运行很多时候并不会完全按照程序员的设想进行,编写代码时进行可控的异常处理机制是十分必要的。通常,对于一个特定的操作,程序员可以定义一个继承自ErrorType的枚举来进行异常类型的描述,使用throw关键字来进行异常的抛出,示例代码如下: //定义一个自定义的错误类型 enum MyError:ErrorType { case DesTroyError case NormalError case SimpleError } //进行异常的抛出 throw MyError.NormalError 函数可以进行错误的传递,需要使用throws关键字来声明这个函数可能会抛出错误,如果不如此声明,则函数内部抛出的错误只能在函数内部解决,throws关键字标记的函数内部抛出的错误会被传递到调用函数的地方,开发者可以在调用函数的地方捕获到错误描述来做相应处理,示例如下: func MyFunc()throws -> Void { throw MyError.NormalError } 对于可能抛出异常的函数调用,开发者要么在调用函数的地方捕获处理这些异常,要么使用try关键字将异常继续抛出去,等待下一层捕获者处理。异常的处理后面会介绍,继续抛出异常示例如下: try MyFunc() 二、异常的处理 除了将错误继续向上抛出之外

Swift Error Handling For Methods That Do Not Throw [duplicate]

冷暖自知 提交于 2019-11-30 22:04:58
问题 This question already has answers here : How do I catch “Index out of range” in Swift? (4 answers) Closed 2 years ago . How to handle errors for methods or code that does not explicitly throw? Wrapping it a do / catch block results in a compiler warning: "'catch' block is unreachable because no errors are thrown in 'do' block" Coming from C# / JAVA background this is an oddity to say the least. As a developer I should be able to safeguard and wrap any code block in do/catch block. Just