try / catch语句VS AppDomain.UnhandledException登录和放大器;应用程序崩溃 [英] try/catch vs AppDomain.UnhandledException to log & crash the application

查看:204
本文介绍了try / catch语句VS AppDomain.UnhandledException登录和放大器;应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,你应该只使用try / catch语句时,你会真正处理异常,而不仅仅是报告和放大器;登录,然后应用程序崩溃。否则,你最好只是检查不同的场景,这是有道理的(例如,如果某事== NULL),或者 - 如果你的目的只是为了记录异常和应用程序崩溃 - 使用的 AppDomain.UnhandledException 。但是,这是总是这样,为什么?

假设下面的方法,它接受的阵列,做一些数据库和文件系统操作之后返回的MemoryStream。

 的MemoryStream读(INT []标识)
{
    尝试
    {
        使用(SqlConnection的连接=新的SqlConnection(connection_string))
        {
            connection.Open();

                        //一堆code执行SQL查询和放大器;构建的MemoryStream,这是在该块的结束返回
        }
    }
    赶上(例外五)
    {
        //报告该异常到用户安培;记录它
        扔; // pointles?
    }
}
 

有可被视为特殊/不必要的行为多种情况,如:

  • 参数(ID为[])为空,
  • 未能建立一个SQL连接,
  • 未能执行特定的SQL查询。

所有这些情况下,被认为是出色的,还是把一切都放到一个try / catch里面,如果你只是想记录异常(然后崩溃)可能是不好的做法 - 但为什么呢?什么是在上述情况下的最佳操控行为?完全避免的try / catch,检查空引用使用if语句(返回在这种情况下空),并使用AppDomain.UnhandledException记录一切?使用try / catch语句,但还是检查空引用使用if语句(并返回在这种情况下)里面?别的东西?

解决方案
  

您应该只使用try / catch语句时,你会真正处理异常,而不仅仅是报告和放大器;登录,然后应用程序崩溃

我同意第一部分,但我想补充一点,把在层的边界记录是有价值的,当你可能没有在调用层控制。例如我登录时出现在顶部的方法Web服务所有的异常,以确保我有记录以来的调试信息服务器(堆栈跟踪等)上并不总是跨通讯层摆好

在你的特殊的例子,我会检查例外条件的在这里您可以的,但让其他异常出现自然。为了您的具体的例子:

  
      
  • 参数(ID为[])为空,
  •   
  • 未能建立一个SQL连接,
  •   
  • 未能执行特定的SQL查询。
  •   

对于第一个,我会检查参数为一个原因:A 的NullReferenceException 给你的没有的背景下对异常的原因,其他的的其中的发生。我更preFER检查,然后抛出一个新的 ArgumentNullException 异常,因为你可以添加的其中的参数为空。您可能仍然需要做一些挖掘,找出的为什么的是空,但它可以节省你大量的时间进行调试。

SQL异常通常可以泡了自然,因为他们有他们的体面的错误信息(例如:未声明的变量'@arg'

As far as I know, you are supposed to only use try/catch when you will actually handle the exception and not just report&log it and then crash the application. Otherwise, you are better off just checking different scenarios where it makes sense (e.g. if sth==null) or - if your purpose is just to log the exception and crash the application - to use AppDomain.UnhandledException. But is this always the case and why?

Suppose the following method, which accepts an array and returns MemoryStream after doing some database and filesystem operations.

MemoryStream Read (int[] IDs)
{
    try
    {
        using (SqlConnection connection = new SqlConnection(connection_string))
        {
            connection.Open();    

                        // a bunch of code executing SQL queries & constructing MemoryStream, which is returned at the end of the block
        }
    }
    catch (Exception e)
    {
        // report the exception to the user & log it
        throw; // pointles??                
    }
}

There are multiple situations that can be considered exceptional/unwanted behavior, such as:

  • argument (IDs[]) being null,
  • failure to establish an SQL connection,
  • failure to execute a specific SQL query.

All these cases are considered exceptional, still putting everything inside a try/catch if you only want to log an exception (then crash) is probably bad practice - but why? What would be the best handling behavior in the above case? Avoid try/catch completely, check null references using an if statement (return null in such case) and use AppDomain.UnhandledException to log everything else? Use try/catch, but still check for null references inside using if statements (and return in that case)? Something else?

解决方案

you are supposed to only use try/catch when you will actually handle the exception and not just report & log it and then crash the application

I agree with the first part, although I would add that adding logging at the tier boundaries is valuable when you may not have control over the calling tier. e.g. I log all exceptions that occur in a Web Service at the top method to ensure I have logging on the server since debug info (stack trace, etc) does not always cross comm layers gracefully

In your particular example I would check for "exceptional" conditions where you can but let other exception occur "naturally". For your specific examples:

  • argument (IDs[]) being null,
  • failure to establish an SQL connection,
  • failure to execute a specific SQL query.

For the first one, I would check for null arguments for one reason: A NullReferenceException gives you no context about the cause of the exception, other that where it occurs. I much prefer to check for null and then throw a new ArgumentNullException exception since you can add which argument is null. You may still need to do some digging to find out why it's null but it saves you a lot of time in debugging.

SQL Exceptions can typically bubble up naturally, since they have decent error information in them (e.g. "undeclared variable '@arg'")

这篇关于try / catch语句VS AppDomain.UnhandledException登录和放大器;应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆