25

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.

  • 1
    @tieTYT guessing people think its something easy to look up? – Daniel Jul 03 '13 at 19:34
  • 2
    @Daniel I see. I didn't know that was a downvote-able offense. – Daniel Kaplan Jul 03 '13 at 19:35
  • 5
    I don't think this question is quite as bad as the voting suggests - Java does have a separate `Throwable` class from it's `Exception` class, which isn't something that maps to C# directly. "Looking up" something that doesn't exist isn't always simple, especially if you expect it to be somewhere... – Reed Copsey Jul 03 '13 at 19:36
  • 1
    No vote on question either (also looks useful) - adding one or two lines explaining/showing what throwable means/used for would probably make it much better. – Alexei Levenkov Jul 03 '13 at 19:47
  • 3
    @tieTYT the downvote tooltip says `does not show any research effort` – Daniel Jul 03 '13 at 19:52

2 Answers2

22

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

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • (Can't vote as I don't know what "throwable" is - shame). Can't you throw anything you want in C++/CLI? I thought that `Exception` restriction is only for Common Language Specification - compliant code, but not .Net in general... – Alexei Levenkov Jul 03 '13 at 19:45
  • @AlexeiLevenkov I don't believe C++/CLI allows that, either - the CLR will allow you to throw any object, but the CLS requires the object to derive from `Exception`. I don't believe any current languages (that I've seen) let you throw a non-exception, other than null. – Reed Copsey Jul 03 '13 at 20:05
  • Searched to see if possible - MSDN says it is i.e. in [String (C++ Component Extensions)](http://msdn.microsoft.com/en-us/library/vstudio/ms177218.aspx). I'm not sure how it actually work inside... maybe it indeed converts it to/from exception. – Alexei Levenkov Jul 03 '13 at 20:19
  • @AlexeiLevenkov I believe that's for native exception handling support - not 100% sure though. – Reed Copsey Jul 03 '13 at 21:03
1

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

Community
  • 1
  • 1
Bruno Martinez
  • 2,850
  • 2
  • 39
  • 47