Abhishek Srivastava said:
You are right. If my sole purpose was to rethrow the exception, then I
will not have the catch block in the first place.
So what value addition does this particular peice of syntax in the C#
langauge do?
The only reason people put catch block is that that they want to do
something with the exception, either to log it, or transform it into
another exception etc... but then I cannot use the above syntax.
Just wondering why is it present as a part of the C# language.
For the sake of completeness, I would suppose. An empty catch block would
most often be used in situations like
string x;
try
{
x = int.Parse(Console.ReadLine());
}
catch
{
}
Console.WriteLine(x);
Its rare, but sometimes you may want to catch *all* exceptions, but don't
really care about them, you simply don't want them to leak upwards. This
syntax should be used rarely, but its existance is there for the few cases
where it is of use.
Also, although I don't know how how much value it would really have, the clr
itself has no restriction on what is thrown with the throw instruction, the
restriction to use System.Exception is enforced by the CLS. The general
catch is actually typed System.Object, allowing you to catch exceptions
thrown by non-CLS compliant languages, even if you can't access the object
directly. C# doesn't allow you to arbitrarily choose your catch type, so a
general catch is the only wayt to catch an exception that *isn't* based on
System.Exception.