Iphone 核心数据-希望出现错误,获得异常
我正在开发一款使用核心数据的iPhone应用程序,ahev注意到,当我预期会出现错误时,我会遇到异常,这是一个示例:Iphone 核心数据-希望出现错误,获得异常,iphone,exception,core-data,Iphone,Exception,Core Data,我正在开发一款使用核心数据的iPhone应用程序,ahev注意到,当我预期会出现错误时,我会遇到异常,这是一个示例: NSError *error; // exception here if edited attribute name todoText in modeller and generated new database with that new name in it(ie clearing the iphone), ie tring to access a field not in
NSError *error;
// exception here if edited attribute name todoText in modeller and generated new database with that new name in it(ie clearing the iphone), ie tring to access a field not in the database
@try {
NSMutableArray *mutableFetchResults = [[todoListManagedObjectContext executeFetchRequest:request error:&error] mutableCopy];
//seems like vars declaered inside a try is only known inside it, so process here
if (mutableFetchResults == nil) { // nil = error
// Handle the error.
正如在评论中所看到的,我没有因为尝试访问一个不存在的字段而得到一个错误……为什么不使用错误返回系统呢
我想我的问题是,什么是错误和异常,我真的需要像这样测试它们吗
Rgds
下午你有什么例外?在整个Cocoa中,异常通常意味着程序员错误,因此它表明您传递的参数无效。试图访问数据库中不存在的字段似乎就是这种情况;这不是“出错”错误,而是“你做错了”错误。谢谢格雷厄姆,谢谢你提供的信息!那么,什么会在这里产生错误呢?这在iPhone上有可能吗?Oe实际会发生什么?因此除了在开发过程中进行调试外,几乎不需要测试异常?关键是要做好处理错误的准备,因为它们可能在应用过程中出错。异常应该在开发时剔除,因为这意味着您在API方面做了错误的事情。没有Cocoa API抛出来指示合理的错误条件。啊,正是我需要的信息,谢谢!(还不能投票支持你的答案…)