using catch

  • Thread starter Thread starter Abhishek Srivastava
  • Start date Start date
A

Abhishek Srivastava

Hello All,

I have seen code snippets like

try
{
.....
}
catch
{
throw;
}

What I wonder is that in this type of syntax, how does one log (on the
console) the exception before throwing it? since we are not using any
name of the exception?

Thanks for your help.

regards,
Abhishek.
 
Abhishek Srivastava said:
Hello All,

I have seen code snippets like

try
{
.....
}
catch
{
throw;
}

What I wonder is that in this type of syntax, how does one log (on the
console) the exception before throwing it? since we are not using any name
of the exception?

You don't, this particular example is mainly useful for effectivly ignoring
an exception(and if you intend to just throw; in the catch, you shouldn't
bother with the catch at all). Most of the time, general catch blocks are a
bad idea, catch (Exception e) { //logging code; throw; } is a *far* better
idea.
 
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.

regards,
Abhishek.
 
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.
 
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.

regards,
Abhishek.

It's not uncommon to have to do some error processing without knowing what
the exact problem was - you might have some cleanup that might otherwise not
get done, for example, or you may just wish to tell someone there was a
problem. C++ has the catch(...) that does the same.

Steve
 
Back
Top