473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

using catch

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.
Nov 15 '05 #1
4 4324

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...
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.

Thanks for your help.

regards,
Abhishek.

Nov 15 '05 #2
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.

Daniel O'Connell [C# MVP] wrote:
"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...
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.
Thanks for your help.

regards,
Abhishek.


Nov 15 '05 #3

"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.
regards,
Abhishek.

Daniel O'Connell [C# MVP] wrote:
"Abhishek Srivastava" <ab*****************@nospam.net> wrote in message
news:Ol**************@TK2MSFTNGP12.phx.gbl...
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.
Thanks for your help.

regards,
Abhishek.


Nov 15 '05 #4
> 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
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
6
by: cody | last post by:
Mostly always you use the using clause you deal with native ressources, so exception handlinjg is sooner or later inevitable. so why do we need to write try { using (File f =...
1
by: gemel | last post by:
I wish to implement error recovery using the Exception mechanism However, I'm not sure I fully appreciate some of the detail in which this works. The code that contains a potential error is...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
3
by: Fred Chateau | last post by:
To catch an exception associated with resources within a Using block, does the Catch block go within the scope of the Using block or following it? If within the scope of the Using block, what...
24
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass();...
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.