【问题标题】:Are All Exception Types Caught Under Plain Old "Exception"?是否所有异常类型都包含在普通的旧“异常”下?
【发布时间】:2011-04-14 18:32:12
【问题描述】:

我只是想更好地理解这一点。

我知道有很多不同的exception types,根据我所做的一些阅读,所有异常类型都被异常捕获。首先,我能确定这是真的吗?

try{
    ...
}
catch(Exception x){
    //No matter what fails in the try block, x 
    //will always have a value since all exception
    //types are caught under Exception? I guess
    //What I really want to know, is will this ever
    //Fail?
}
catch(SystemException x){
    //This code will never execute since all 
    //exceptions are caught in the first catch?
}

接下来,这个捕捉层次结构是如何工作的?如果 Exception 位于顶部,那么其他所有异常类型是否都在 Exception 下一级,或者是否存在多个类型层,例如 Exception 是否是 ExceptionSomething 的父级,它是 ExceptionSomethingElse 的父级?

附录:

或者如果我们有这样的代码:

try{
    ...
}
catch(SystemException x){
    //If there is an exception that is not a SystemException
    //code in this block will not run, right (since this is 
    //looking specifically for SystemExceptions)?
}

【问题讨论】:

    标签: c# exception


    【解决方案1】:

    这实际上取决于您的 .NET 版本和配置。在 C++/CLI 中,你可以抛出 anything;它不必是Exception。在 1.1 (IIRC) 中,您只能使用以下 catch 块捕获这些:

    catch {...}
    

    这不是很有帮助 - 你看不到发生了什么。在 2.0 (IIRC) 默认情况下,此类异常会自动包装在一个虚拟的 RuntimeWrappedException 子类中。但是,为了兼容性,您可以将其关闭。我求求你:不要:)

    【讨论】:

    • 如果我不添加任何会引发任何疯狂的代码,那么仅捕获异常应该就足够了吗?
    • @sooprise 是的;在任何理智的代码中,捕捉异常都很好;但仅用于日志记录等;并记得throw,而不是throw ex重新抛出。
    【解决方案2】:

    是的,这是可行的,因为所有标准异常都继承自Exception。

    您的代码可以工作,但您需要将Exception 的处理程序放在所有专业 异常类型之后。 (将执行第一个匹配的处理程序。)

    不从Exception 继承的异常不会被捕获,因为它们不是指定的类型。但是 .NET 异常类都继承自这个基类。

    有些人认为这不是一个好主意,但我通常只捕获 Exception,除非我想对特定异常类型进行特殊处理。

    【讨论】:

      【解决方案3】:

      是的,Exception 继承自 Object 类,all exceptions 继承自 Exception 类。

      上面的链接将显示所有异常的层次结构。

      System.Object 
        System.Exception
          Microsoft.Build.BuildEngine.InternalLoggerException
          Microsoft.Build.BuildEngine.InvalidProjectFileException
          Microsoft.Build.BuildEngine.InvalidToolsetDefinitionException
          Microsoft.Build.BuildEngine.RemoteErrorException
          ...
      

      其中一些异常,比如你提到的 SystemException 有更多的继承自它们的异常,但它们仍然继承自 Exception 类:

      System.Object 
        System.Exception
          System.SystemException
            Microsoft.SqlServer.Server.InvalidUdtException
            System.AccessViolationException
            System.Activities.ValidationException
            System.AppDomainUnloadedException
            System.ArgumentException
            System.ArithmeticException
            ...
      

      【讨论】:

        【解决方案4】:

        回答您问题的第二部分,.Net Framework 中的异常层次结构示例:

        ArgumentNullException inherits from
        ArgumentException inherits from
        SystemException  inherits from
        Exception     
        

        您应该尽量处理最具体的情况。

        try{
            //something
        }
        catch(ArgumentNullException ex){
            //handle ArgumentNullException
        }
        catch(SystemException ex1)
        {
            //handle other kinds of SystemException
            if(IDontWantToHandleThisExceptionHere(ex1))
            {
                throw;//  not throw new Exception(ex1);
            }
        }
        

        【讨论】:

          【解决方案5】:

          后者,异常可以继承自基类Exception 或任何其他继承基类的类。

          例如:SqlException 继承自 DbException,ExternalException 继承自 SystemException,SystemException 最后继承自 Exception。

          【讨论】:

            【解决方案6】:

            是的,所有异常类型都继承自异常。

            而且,继承的工作方式,您可以拥有多级继承

            MyAppException : Exception {
            }
            
            MyAppFileNotFoundException : MyAppException {
            }
            

            这通常用于具有不同类型的异常的不同行为

            try {
                 openfile('thisFileDoesNotExist.txt');
            }
            catch (MyAppFileNotFoundException ex)
            {
                  //warn the user the file does not exist
            }
            catch (Exception ex)
            {
                  //warn the user an unknown error occurred, log the error, etc
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-08-15
              • 1970-01-01
              • 1970-01-01
              • 2013-09-24
              • 2013-11-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多