与 Java 的 Throwable 等效的 C# 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17456579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:03:45  来源:igfitidea点击:

What is the C# equivalent to Java's Throwable?

c#javaexceptionthrowable

提问by JohnOsborne

What is the C# equivalent to Java's Throwable?

什么是 C# 相当于 Java 的Throwable

In Java, the root of the exception class hierarchy is called Throwable, notException. The Throwablebase class has two derived classes:

在 Java 中,异常类层次结构的根称为Throwable而不是Exception。该Throwable基类有两个派生类:

Exception: for conditions that a reasonable application might want to catch.

Exception: 对于合理的应用程序可能想要捕获的条件。

Error: for serious problems that a reasonable program should not try to catch.

Error: 对于一个合理的程序不应该试图捕捉的严重问题。

So the Throwablebase class includes problems that a reasonable program should not try to catch.

所以Throwable基类包含了一个合理的程序不应该试图捕捉的问题。

采纳答案by Reed Copsey

That would be the Exception class. There is no separate "throwable" concept aside from exceptions in .NET.

那将是Exception 类。除了 .NET 中的异常之外,没有单独的“可抛出”概念。

回答by Bruno Martinez

.Net allows exceptions of any class, but C# restricts throw and catch to Exception. Use a catch clause that specifies neither type nor variable to catch non Exception exceptions.

.Net 允许任何类的异常,但 C# 将 throw 和 catch 限制为异常。使用既不指定类型也不指定变量的 catch 子句来捕获非异常异常。

The relevant spec snippet:

相关的规范片段:

When a catch clause specifies a class-type, the type must be System.Exception, a type that derives from System.Exception or a type parameter type that has System.Exception (or a subclass thereof) as its effective base class.

When a catch clause specifies both a class-type and an identifier, an exception variable of the given name and type is declared. The exception variable corresponds to a local variable with a scope that extends over the catch block. During execution of the catch block, the exception variable represents the exception currently being handled. For purposes of definite assignment checking, the exception variable is considered definitely assigned in its entire scope.

Unless a catch clause includes an exception variable name, it is impossible to access the exception object in the catch block.

A catch clause that specifies neither an exception type nor an exception variable name is called a general catch clause. A try statement can only have one general catch clause, and if one is present it must be the last catch clause.

Some programming languages may support exceptions that are not representable as an object derived from System.Exception, although such exceptions could never be generated by C# code. A general catch clause may be used to catch such exceptions. Thus, a general catch clause is semantically different from one that specifies the type System.Exception, in that the former may also catch exceptions from other languages.

当 catch 子句指定类类型时,该类型必须是 System.Exception、派生自 System.Exception 的类型或具有 System.Exception(或其子类)作为其有效基类的类型参数类型。

当 catch 子句同时指定类类型和标识符时,就会声明一个给定名称和类型的异常变量。异常变量对应于一个局部变量,其作用域扩展到 catch 块。在 catch 块执行期间,异常变量表示当前正在处理的异常。出于明确赋值检查的目的,异常变量被视为在其整个范围内明确赋值。

除非 catch 子句包含异常变量名,否则无法访问 catch 块中的异常对象。

既不指定异常类型也不指定异常变量名的 catch 子句称为通用 catch 子句。一个 try 语句只能有一个通用的 catch 子句,如果有一个,它必须是最后一个 catch 子句。

某些编程语言可能支持无法表示为派生自 System.Exception 的对象的异常,尽管此类异常永远不会由 C# 代码生成。通用 catch 子句可用于捕获此类异常。因此,通用 catch 子句在语义上不同于指定类型 System.Exception 的子句,因为前者也可以捕获来自其他语言的异常。

.Net 4.0 introduces a concept similar to Java's Error class. While Corrupted State Exceptions extend Exception, only methods with HandleProcessCorruptedStateExceptionsAttributecatch CSEs.

.Net 4.0 引入了一个类似于 Java 的 Error 类的概念。虽然损坏状态异常扩展了异常,但只有具有HandleProcessCorruptedStateExceptionsAttribute 的方法才能捕获 CSE。