iPhone生产/发布异常处理
提前请原谅我对 iPhone/Objective-C 最佳实践缺乏了解;我有 .NET/C# 背景。
尽管我读过很多关于 iPhone 异常处理的文章,但我仍然不太清楚大多数人对生产代码做了什么。另外,我还没有找到任何具有我通常期望的错误处理功能的开源应用程序。以下是我的问题:
1)如果发生意外结果导致应用程序最终失败,您会抛出异常还是只是等待它稍后失败?例如,
if (![fileManager createDirectoryAtPath: myNewDir
withIntermediateDirectories: YES
attributes: nil
error: &myError]) {
// My app shouldn't continue. Should I raise an exception?
// Or should I just log it and then wait for it to crash later?
}
2) 您是否验证参数?例如,在 C# 中,我通常会检查 null,如果需要,则抛出 ArgumentNullException。
3)当应用程序崩溃时,是否会自动记录崩溃信息,或者我是否需要设置未处理的异常处理程序?我可以在此方法中显示 UIAlertView 来通知用户发生了一些不好的事情,而不是让应用程序消失吗? (如果是这样,我就无法让它工作。)
4)最后,为什么我没有看到有人真正使用@try/@catch/@finally?它在 C# 中被广泛使用,但我还没有找到使用它的开源应用程序。 (也许我只是看错了应用程序。)
In advance, please excuse my lack of understanding for iPhone/Objective-C Best Practices; I come from a .NET/C# background.
Although, I've read many posts regarding exception handling for the iPhone, I'm still not exactly clear on what most people do for production code. Also, I haven't been able to find any open source apps with error handling that I would normally expect. Here are my questions:
1) If an unexpected result occurs that would cause the application to eventually fail, would you throw an exception or just wait for it to fail later? For example,
if (![fileManager createDirectoryAtPath: myNewDir
withIntermediateDirectories: YES
attributes: nil
error: &myError]) {
// My app shouldn't continue. Should I raise an exception?
// Or should I just log it and then wait for it to crash later?
}
2) Do you validate parameters? For example, in C# I would usually check for null, and if needed throw an ArgumentNullException.
3) When an app crashes, does the crash info get logged automatically or do I need to set the unhandled exception handler? Can I show a UIAlertView within this method to inform the user something bad happened, instead of having the app just disappear? (If so, I couldn't get it to work.)
4) And finally, why don't I see anyone actually using @try/@catch/@finally? It's used extensively in C#, but I haven't found open source apps using it. (Maybe I'm just looking at the wrong apps.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
广告 1. 你给出的例子是一个有点经典的例子,说明何时应该在 Java 中使用“受检查的异常”。该方法的调用者必须做好准备,调用可能会因超出其控制范围的原因而失败。特别是,在给出的示例中,我将允许错误描述符对象允许传播回调用者:
如果错误确实不可恢复,您实际上可能会引发异常,但这也会丧失显示正确错误的任何机会向您的应用程序的用户发送消息。
Ad 2. 是的,参数验证绝对是你应该做的事情。为意外的
nil
引发异常是完全合适的。错误的参数是“程序员的错误”,您无法保证程序仍处于一致的状态。例如,如果您尝试使用不存在的索引获取元素,NSArray
会引发异常。广告 3. 当应用程序由于未捕获的异常而崩溃时,系统会自动记录该事实。如果您确实想显示错误消息,可以捕获异常。但是,应用程序可能处于不一致的状态,并且不应允许其继续运行,因此简单地允许它停止运行似乎是最好的解决方案。
广告4。我不知道。
Ad 1. The example you give is a somewhat classical example of when one should use a "checked exception" in Java. The caller of the method has to be prepared, that the call can fail for reasons which are outside of what it can control. In particular, in the example given, I would allow the error descriptor object allow to propagate back to the caller:
If the error is really not recoverable, you may actually raise an exception, but that will also forfeit any chance to show a proper error message to the user of your application.
Ad 2. Yes, parameter validation is definitely something you should do. And raising an exception for unexpected
nil
s is entirely appropriate. A bad parameter is a "programmer's error" and you cannot guarantee, that the program is still in a consistent state. For example,NSArray
raises an exception, if you try to get an element using an non-existent index.Ad 3. When the app crashes due to an uncaught exception, the fact is logged automatically. You can catch the exception if you really want to display an error message. However, the app is likely to be in an inconsistent state, and should not be allowed to continue, so simply allowing it to go down seems the best solution here.
Ad 4. I don't know.
[NSString stringWithString:]
周围放置 @try/@catch 块,因为它抛出异常的唯一方法是尝试传递nil< /代码>。确保您没有传递
nil
,这样您就不必担心捕获异常。当一个方法可能因为你无法控制的原因而失败时,它们通常通过 NSError 进行通信。例如,请参阅NSManagedObjectContext
的- (BOOL)save:(NSError **)error
方法。nil
(nothing happens if you do) so in a lot of situations you don't need to check for nil. If I'm in a model class I always validate parameters in methods. If I'm not I almost never validate anything, type checking is good enough.[NSString stringWithString:]
because the only way it'll throw an exception is if you try and passnil
. Make sure you aren't passingnil
and you won't have to worry about catching exceptions. When a method might fail because of something outside of your control, they usually communicate via an NSError. See the- (BOOL)save:(NSError **)error
method ofNSManagedObjectContext
for example.