No one has yet mentioned the historical aspect of this question.
In .NET it is legal to throw an object that does not derive from Exception
. (It is not legal in C#, but it is in some other managed languages.) Many people are unaware of this fact, but it is legal. Since that is crazy, in .NET 2.0 the default was changed: if you attempt to throw something that is not an exception then it is automatically wrapped in the RuntimeWrappedException
class which obviously is an exception. That object is then thrown.
Because of this oddity, in C# 1.0 it was common to see code that did both:
try
{ do something }
catch(Exception)
{ handle the exception }
catch
{ handle the thrown non-exception }
And in fact there were security and correctness issues; there are situations in which for security reasons you must catch anything that is thrown (possibly to re-throw it) and people would think reasonably that catch(Exception)
caught everything, but it didn't.
Fortunately since .NET 2.0 things have been more sensible; you can rely on catch {}
, catch(Exception) {}
and catch(Exception ex) {}
to all catch everything should you need to.
And finally: if for some crazy reason you want to turn on the C# 1.0 behavior, you can put
[assembly:System.Runtime.CompilerServices.RuntimeCompatibility(WrapNonExceptionThrows = false)]
in your program.