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

2

26

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.

Divest answered 3/7, 2013 at 19:30 Comment(5)
@tieTYT guessing people think its something easy to look up?Colonial
@Colonial I see. I didn't know that was a downvote-able offense.Amphipod
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...Ms
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.Hallvard
@tieTYT the downvote tooltip says does not show any research effortColonial
M
22

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

Ms answered 3/7, 2013 at 19:31 Comment(4)
(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...Hallvard
@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.Ms
Searched to see if possible - MSDN says it is i.e. in String (C++ Component Extensions). I'm not sure how it actually work inside... maybe it indeed converts it to/from exception.Hallvard
@AlexeiLevenkov I believe that's for native exception handling support - not 100% sure though.Ms
P
2

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

Parted answered 28/9, 2014 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.