Frage

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

In Java, the root of the exception class hierarchy is called Throwable, not Exception. The Throwable base class has two derived classes:

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

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

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

War es hilfreich?

Lösung

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

Andere Tipps

.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.

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.

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top