473,397 Members | 2,033 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,397 software developers and data experts.

try catch overhead.

Hi,

Is there any overhead attached with the try catch block? In C# everything is
an exception. So is it OK to have a try catch block for each function ??? or
is there any other way. What is the correct /efficient programming
practise??
Is there any other article available on the same?

Thanks for your time.

Regards,
Gobi.
Dec 6 '07 #1
13 4507
On 6 Dez., 07:28, "Gobinath" <gobinat...@coolgoose.comwrote:
Hi,

Is there any overhead attached with the try catch block? In C# everything is
an exception. So is it OK to have a try catch block for each function ??? or
is there any other way. What is the correct /efficient programming
practise??
Is there any other article available on the same?

Thanks for your time.

Regards,
Gobi.
As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.
The obvious exception from this is when you anticipate a certain
exception and are able to recover from it, then you would wrap the
smallest possible piece of code in the try catch and only catch the
exceptions that you know you can recover from.
If you want more information on exceptions you should check out Jon
Skeet's homepage, he has a great collection of really useful articles
on many c# topics:
http://www.yoda.arachsys.com/csharp/exceptions2.html

hth,
Kevin Wienhold
Dec 6 '07 #2
From: KWienhold [mailto:he******@trashmail.net]
Posted At: Thursday, 6 December 2007 3:47 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: try catch overhead.
Subject: Re: try catch overhead.
*snip*
As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.
*snip*

Hey KWien,

You mentioned error-logging be the global error handler. All I had
observed so far was the visible dialogue that comes up and "annoys" the
customer, who then proceeds to close it before calling to say something
was wrong.

Where would I find the error-log information? Event Viewer? A local
file? Within the Windows directory somewhere?

This would be very beneficial to know for debugging at the clients site
once they have closed the Exception screen and can't reproduce the
error.

Regards,
Wingot

Dec 6 '07 #3
Gobinath... Well, not _everything_ is an exception as C# does support
the Try,
out pattern as in Double.TryParse :)

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 6 '07 #4
On Wed, 05 Dec 2007 23:02:51 -0800, Wingot <wi****@newsgroup.nospamwrote:
Hey KWien,

You mentioned error-logging be the global error handler. All I had
observed so far was the visible dialogue that comes up and "annoys" the
customer, who then proceeds to close it before calling to say something
was wrong.

Where would I find the error-log information? Event Viewer? A local
file? Within the Windows directory somewhere?
I believe Kevin is referring to whatever custom error logging an
application might provide. His point is that you would only handle an
exception at a point in the code where you can do something useful about
it. If that "something" includes logging the exception, it would be
wherever the application designer decided to put it. The event log would
be a natural location, but it could be a local file or even transmitted to
a server somewhere.

For some exceptions, this handling may well be near the top level of your
code and it may be that the only useful thing to do is present the error
to the user (for example, by displaying an informational message, along
with the Exception.Message property value, in a MessageBox.Show()
statement), and/or logging the error somewhere as the developer sees fit,
and then just continuing to allow the user to use the application normally.

For other exceptions, it might be there's a sensible way to handle the
error at a deeper level in the code. For example, some sort of data entry
or i/o where the input data is invalid but some default can be provided
instead in the event of an exception. Again, the user may or may not be
alerted, the exception may or may not be logged, but any of that would be
up to the person writing the code.

As far as I know, there is no automated exception logging facility that
handles this sort of thing. It would be something implemented by the
developer of the program.

Pete
Dec 6 '07 #5
eps
Peter Duniho wrote:
As far as I know, there is no automated exception logging facility that
handles this sort of thing. It would be something implemented by the
developer of the program.

Pete
As you say there are lots of ways of doing this, I recently saw the
following on a code snippets site :

// Put this at the top
using System.IO;
//

public static void Log(string Message)
{
File.AppendAllText(HttpContext.Current.Server.MapP ath("~") +
"/log.txt", DateTime.Now.ToShortDateString() + " " +
DateTime.Now.ToShortTimeString() + ": " + Message + Environment.NewLine);
}
It impressed me due to the fact that its basically just one line (ok the
example above is meant for asp) I have used this in a c# class
library like so...

catch (Exception e)
{
File.AppendAllText(@"c:\log.txt", e.Message);
}
for a production system you may want to do a bit more but the above is
great for capturing exceptions whilst developing without crashing the app.

The code was taken from http://snippets.dzone.com/posts/show/4334

--
Eps
Dec 6 '07 #6
On 6 Dez., 08:02, "Wingot" <win...@newsgroup.nospamwrote:
From: KWienhold [mailto:hedov...@trashmail.net]
Posted At: Thursday, 6 December 2007 3:47 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: try catch overhead.
Subject: Re: try catch overhead.
*snip*
As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.

*snip*

Hey KWien,

You mentioned error-logging be the global error handler. All I had
observed so far was the visible dialogue that comes up and "annoys" the
customer, who then proceeds to close it before calling to say something
was wrong.

Where would I find the error-log information? Event Viewer? A local
file? Within the Windows directory somewhere?

This would be very beneficial to know for debugging at the clients site
once they have closed the Exception screen and can't reproduce the
error.

Regards,
Wingot
As Peter already mentioned, logging is normally implemented by the
developer, so it could be stored in many different locations.
Unfortunately many applications actually don't log their errors, but I
figured we were talking about applications that we wrote ourselves, so
its totally up to you.
Logging can be extremely useful for debugging purposes, as you pointed
out, since users are prone to just clicking "OK".
Another benefit is that you don't actually have to display the stack-
trace to the user (like MessageBox.Show("{0}", ex) would do), but you
can just dump it into a file and still have it available.
You may not think something like that is important, but after
explaining to a rather important customer what the method
"SpeedyGonzales.GoGoGo()" was (yeah, I have some great colleagues), I
really appreciate it ;)

On a sidenote, if you want to implement global error handling for
logging purposes you may want to take a look at the

AppDomain.CurrentDomain.UnhandledException
System.Windows.Forms.Application.ThreadException

events.

Kevin Wienhold
Dec 6 '07 #7
Gobinath,

As there are *To much* Try and Catch blocks used in a class, then it is for
me a sign that it is done by a beginner who did not understand why something
did not work, and fixed that with a Try and Catch to set some mostly wrong
values as result.

Just my idea about this.

Cor

Dec 7 '07 #8
Thanks a lot for all your response.. I think I have a better feeling for it
now..

Thanks a lot for your time.

Regards,
Gobi.
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:1B**********************************@microsof t.com...
Gobinath,

As there are *To much* Try and Catch blocks used in a class, then it is
for me a sign that it is done by a beginner who did not understand why
something did not work, and fixed that with a Try and Catch to set some
mostly wrong values as result.

Just my idea about this.

Cor

Dec 7 '07 #9
KWienhold wrote:
As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.
I don't agree with that for a large application.

I a large real world app then log+display+exit is likely not
good enough.

And in a multi layered component based world then the top level
is very unlikely to be able to do anything, because what has to be
done in case of an exception is also encapsulated somewhere.

Arne
Dec 8 '07 #10
Arne Vajhøj <ar**@vajhoej.dkwrote:
KWienhold wrote:
As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.
I don't agree with that for a large application.

I a large real world app then log+display+exit is likely not
good enough.
And in a multi layered component based world then the top level
is very unlikely to be able to do anything, because what has to be
done in case of an exception is also encapsulated somewhere.
In a large application, you'd typically have each largish component or
perhaps each layer being responsible for dealing with exceptions.
However, you certainly *shouldn't* say "It's a large app, therefore
every single method should have its own try/catch block."

There should still be relatively few try/catch blocks even in a large
app, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 8 '07 #11
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>KWienhold wrote:
>>As long as there is no actual exception, the overhead is very small.
However, wrapping every single function into a try/catch block would
be somewhat unorthodox. Normally you let exceptions "bubble up" to a
global error handler on the top level and let that handle the error-
logging and display error information.
I don't agree with that for a large application.

I a large real world app then log+display+exit is likely not
good enough.
And in a multi layered component based world then the top level
is very unlikely to be able to do anything, because what has to be
done in case of an exception is also encapsulated somewhere.

In a large application, you'd typically have each largish component or
perhaps each layer being responsible for dealing with exceptions.
However, you certainly *shouldn't* say "It's a large app, therefore
every single method should have its own try/catch block."

There should still be relatively few try/catch blocks even in a large
app, IMO.
I don't think I have said otherwise.

I just object to the "global".

Arne

Dec 8 '07 #12
On Dec 8, 10:08 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
There should still be relatively few try/catch blocks even in a large
app, IMO.

I don't think I have said otherwise.

I just object to the "global".
It would have been helpful to have specified exactly which part you
didn't agree with then, rather than just quoting the whole response
and saying "I don't agree with that for a large application." It looks
like you disagree with the whole response rather than just one or two
words.

Jon
Dec 8 '07 #13
Jon Skeet [C# MVP] wrote:
On Dec 8, 10:08 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>>There should still be relatively few try/catch blocks even in a large
app, IMO.
I don't think I have said otherwise.

I just object to the "global".

It would have been helpful to have specified exactly which part you
didn't agree with then, rather than just quoting the whole response
and saying "I don't agree with that for a large application." It looks
like you disagree with the whole response rather than just one or two
words.
I quoted:

# As long as there is no actual exception, the overhead is very small.
# However, wrapping every single function into a try/catch block would
# be somewhat unorthodox. Normally you let exceptions "bubble up" to a
# global error handler on the top level and let that handle the error-
# logging and display error information.

I could have omitted the first two and a half line or put in a
"so far I agree".

Instead I have people the opportunity to deduce that from
the argumentation what more specifically I disagreed with.

That strategy did not work very well.

I now understand that people can read:

#I a large real world app then log+display+exit is likely not
#good enough.
#
#And in a multi layered component based world then the top level
#is very unlikely to be able to do anything, because what has to be
#done in case of an exception is also encapsulated somewhere.

As an argument for a try catch in every method.

Arne
Dec 9 '07 #14

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

Similar topics

4
by: Chris Martin | last post by:
Below are three try-catch blocks. My question is, how can all three work, and how come there is no memory leak (or is there)? As I understand it, an exception object is being created and "thrown"...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
22
by: STom | last post by:
I heard someone mention to me that the use of try catch exception handling is very expensive (in relative terms of slowing an app down) if it is used frequently. Of course they could not explain...
9
by: Bob Achgill | last post by:
I really like this function but have tried to slow down on using it because I get a 1 second pause each time I use it. I don't really understand why the computer has to think for 1 second! ...
3
by: ari | last post by:
hey all, i know i need a try catch in my method but do i put all the contents of my method withing the try catch or should I just put some of the code? thanks, ari
5
by: Back 9 | last post by:
I try to insert try/catch statement in every single method of my c# code to prevent the app from crashing not nicely. Do you think it is good idea? I remember that an artical says " do not overuse...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
9
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I have a class with an collection property. The collection property is sometimes equal to nothing. The class has a function named 'Exists' which returns TRUE if the specified string exists...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.