简体   繁体   English

iPhone生产/发布异常处理

[英]iPhone Production/Release Exception Handling

In advance, please excuse my lack of understanding for iPhone/Objective-C Best Practices; 事先,请原谅我对iPhone / Objective-C最佳做法缺乏了解; I come from a .NET/C# background. 我来自.NET / C#背景。

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. 尽管我已经阅读了很多有关iPhone异常处理的文章,但是我仍然不清楚大多数人对生产代码的处理方式。 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? 1)如果发生意想不到的结果,最终导致应用程序失败,您会抛出异常还是只是等待其以后失败? 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? 2)您是否验证参数? For example, in C# I would usually check for null, and if needed throw an ArgumentNullException. 例如,在C#中,我通常会检查是否为null,如果需要,则抛出ArgumentNullException。

3) When an app crashes, does the crash info get logged automatically or do I need to set the unhandled exception handler? 3)当应用崩溃时,崩溃信息会自动记录下来还是我需要设置未处理的异常处理程序? Can I show a UIAlertView within this method to inform the user something bad happened, instead of having the app just disappear? 我可以在此方法中显示UIAlertView来通知用户发生了什么不好的事情,而不是让应用程序消失吗? (If so, I couldn't get it to work.) (如果是这样,我将无法正常工作。)

4) And finally, why don't I see anyone actually using @try/@catch/@finally? 4)最后,为什么我看不到有人实际使用@ try / @ catch / @ finally? It's used extensively in C#, but I haven't found open source apps using it. 它在C#中得到了广泛的使用,但是我还没有找到使用它的开源应用程序。 (Maybe I'm just looking at the wrong apps.) (也许我只是看错了应用程序。)

Ad 1. The example you give is a somewhat classical example of when one should use a "checked exception" in Java. 广告1。您提供的示例是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: 特别是,在给出的示例中,我将允许错误描述符对象允许传播回调用者:

- (BOOL) prepareDirectories: (NSString*) path error: (NSError**) location {

    if( ![fileManager createDirectoryAtPath: path
                      withIntermediateDirectories: YES 
                      attributes: nil 
                      error: location] ) {

        return NO;
    }

    // ... additional set-up here
    return YES;
}

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. 广告2。是的,参数验证绝对是您应该做的事情。 And raising an exception for unexpected nil s is entirely appropriate. 为意外的nil引发异常是完全适当的。 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. 例如,如果尝试使用不存在的索引获取元素,则NSArray会引发异常。

Ad 3. When the app crashes due to an uncaught exception, the fact is logged automatically. 广告3.当应用程序由于未捕获的异常而崩溃时,该事实将自动记录。 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. 广告4.我不知道。

  1. In the specific case you mention, you should present a notification to the user about what just happened with instructions on how to fix the situation. 在您提到的特定情况下,您应该向用户显示有关刚刚发生的情况的通知,并提供有关如何解决此情况的说明。 Except in extreme circumstances your app should not quit. 除非在极端情况下,否则您的应用程序不应退出。 You should let the user fix whatever is wrong, then try again. 您应该让用户修复任何错误,然后重试。
  2. Parameter validation depends on a lot of factors. 参数验证取决于很多因素。 One thing to keep in mind is that in Obj-C it's perfectly valid to send messages to nil (nothing happens if you do) so in a lot of situations you don't need to check for nil. 要记住的一件事是,在Obj-C中将消息发送到nil是完全有效的(如果您这样做则什么也不会发生),因此在很多情况下,您不需要检查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. 如果我不是,我几乎从不验证任何内容,那么类型检查就足够了。
  3. Some information should be logged but it's always helpful to log more specific information to your situation. 应该记录一些信息,但是根据情况记录更具体的信息总是有帮助的。 Ideally you shouldn't ever reach the unhandled exception state. 理想情况下,您永远都不应达到未处理的异常状态。
  4. Cocoa classes will rarely throw exceptions for environmental reasons, they are almost always because you did something wrong. 可可类很少出于环境原因而引发异常,它们几乎总是因为您做错了什么。 For example, you wouldn't usually put a @try/@catch block around [NSString stringWithString:] because the only way it'll throw an exception is if you try and pass nil . 例如,通常不会在[NSString stringWithString:]周围放置@ try / @ catch块,因为它会引发异常的唯一方法是尝试传递nil Make sure you aren't passing nil and you won't have to worry about catching exceptions. 确保您没有传递nil并且您不必担心捕获异常。 When a method might fail because of something outside of your control, they usually communicate via an NSError. 当某个方法可能由于您无法控制的原因而失败时,它们通常会通过NSError进行通信。 See the - (BOOL)save:(NSError **)error method of NSManagedObjectContext for example. 例如,请参见NSManagedObjectContext- (BOOL)save:(NSError **)error方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM