473,569 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Try / Catch - Performance and best-practices


Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such a
situation??

Thanks a lot
Daniel Portal
Nov 17 '05 #1
8 7808
Daniel,

You shouldn't do what you are doing. Basically, you should try and
catch only the exceptions you are expecting to get (and even then, if there
is another way to test for that condition, you should do so).

Otherwise, yes, you are going to place a huge strain on your system.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Daniel Portal" <it********@bol .com.br> wrote in message
news:er******** ******@TK2MSFTN GP12.phx.gbl...

Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such a
situation??

Thanks a lot
Daniel Portal

Nov 17 '05 #2
Some exceptions should be thrown to the caller. Business classes, for
example, should handle those exceptions they can handle gracefully, and pass
them up (by throwing) to the caller, so that the caller can handle it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Daniel Portal" <it********@bol .com.br> wrote in message
news:er******** ******@TK2MSFTN GP12.phx.gbl...

Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such a
situation??

Thanks a lot
Daniel Portal

Nov 17 '05 #3
Unless I am missing some business specific purpose, you wouldnt gain
anything from doing a try/catch in every statement. Depending on your
scenario, you might not kill your speed -- but it certainly is a lot of
code that you probably wouldnt get any benefit for (and not to mention a
lot of additional testing because of the increased cyclomatic complexity)

In the past VB developers (not .NET) had on errors in every method
because that was the easiest way to build up a stack trace for tracking
down errors .... but with the StackTrace property of Exception that is
no longer necessary.
Daniel Portal wrote:
Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such a
situation??

Thanks a lot
Daniel Portal

Nov 17 '05 #4
If it's not asking too much, is that possible to provide some official
article or reference that un-recommend this practice, or explains why not to
do it?

Thx

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** **********@TK2M SFTNGP15.phx.gb l...
Daniel,

You shouldn't do what you are doing. Basically, you should try and
catch only the exceptions you are expecting to get (and even then, if
there is another way to test for that condition, you should do so).

Otherwise, yes, you are going to place a huge strain on your system.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Daniel Portal" <it********@bol .com.br> wrote in message
news:er******** ******@TK2MSFTN GP12.phx.gbl...

Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such a
situation??

Thanks a lot
Daniel Portal


Nov 17 '05 #5
Here you go, Daniel:

http://msdn.microsoft.com/library/de...ndamentals.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.

"Daniel Portal" <it********@bol .com.br> wrote in message
news:O$******** ******@TK2MSFTN GP09.phx.gbl...
If it's not asking too much, is that possible to provide some official
article or reference that un-recommend this practice, or explains why not
to
do it?

Thx

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:%2******** **********@TK2M SFTNGP15.phx.gb l...
Daniel,

You shouldn't do what you are doing. Basically, you should try and
catch only the exceptions you are expecting to get (and even then, if
there is another way to test for that condition, you should do so).

Otherwise, yes, you are going to place a huge strain on your system.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Daniel Portal" <it********@bol .com.br> wrote in message
news:er******** ******@TK2MSFTN GP12.phx.gbl...

Hi there,

I'm in 3-tier project and I'm thinking about having Try / Catch
statements in each method, just in case you know!
If the case is that I don't have a special treatment for the error in
some method I would Cath the error and Re-Throw it!

The question is: what are the issues of this approach? Would I have
performance problems cus of my catches? What does MS recommends in such
a situation??

Thanks a lot
Daniel Portal



Nov 17 '05 #6
Daniel Portal <it********@bol .com.br> wrote:
If it's not asking too much, is that possible to provide some official
article or reference that un-recommend this practice, or explains why not to
do it?


Your code becomes unreadable - you've got a much lower signal-to-noise
ratio in your code, because of the extra try/catch in each method,
whether it's required or not.

The performance issue is pretty much irrelevant here, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
The "why not" is fairly straightforward : you gain nothing by catching
an exception that your application can't deal with at that point. If
you search this newsgroup for "try catch exception" or something like
that you'll find lots of threads on best practices. For example, here
are some recent conversations on the topic in which I was involved:

http://groups.google.com/group/micro...4ccb521b5e15e8

http://groups.google.com/group/micro...3571a29979c948

In a nutshell, catch an exception if:

1. The method catching the exception can deal with that specific
condition and continue working.
2. The exception type would be meaningless to your caller, and you want
to "wrap" the exception in a more meaningful exception.
3. You want to log additional information about your local state before
re-throwing the exception. (Either by logging the additional
information or by adding it to a "wrapper" exception.)
4. You want to note the exception and deal with it later.

and a couple of other reasons I can't remember right now. Otherwise,
what have you gained? You caught something you can't deal with, and at
best you just re-throw it. At worst you swallow an important exception.

There are top-level exception handling routines that you can hook into
the handle at the top level of your application all of those exceptions
that had no solution farther down your call stack. Of course, by that
time your application is going down anyway, but that's generally how
you handle exceptions with no remedy: allow them to bubble to the top
of the call stack, catch them in a global handler, log them, and then
allow the app to go down.

Nov 17 '05 #8
Thanks Bruce...

If anyone has more interest in this subject, I found this guy who might be
quite useful for everyone:

http://www.microsoft.com/downloads/d...displaylang=en

And hanks everyone who invested time replying this... kinda dumb question,
but it had to be asked!

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
The "why not" is fairly straightforward : you gain nothing by catching
an exception that your application can't deal with at that point. If
you search this newsgroup for "try catch exception" or something like
that you'll find lots of threads on best practices. For example, here
are some recent conversations on the topic in which I was involved:

http://groups.google.com/group/micro...4ccb521b5e15e8

http://groups.google.com/group/micro...3571a29979c948

In a nutshell, catch an exception if:

1. The method catching the exception can deal with that specific
condition and continue working.
2. The exception type would be meaningless to your caller, and you want
to "wrap" the exception in a more meaningful exception.
3. You want to log additional information about your local state before
re-throwing the exception. (Either by logging the additional
information or by adding it to a "wrapper" exception.)
4. You want to note the exception and deal with it later.

and a couple of other reasons I can't remember right now. Otherwise,
what have you gained? You caught something you can't deal with, and at
best you just re-throw it. At worst you swallow an important exception.

There are top-level exception handling routines that you can hook into
the handle at the top level of your application all of those exceptions
that had no solution farther down your call stack. Of course, by that
time your application is going down anyway, but that's generally how
you handle exceptions with no remedy: allow them to bubble to the top
of the call stack, catch them in a global handler, log them, and then
allow the app to go down.

Nov 17 '05 #9

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

Similar topics

3
1941
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm tending towards the use of generators. One good example is if I want to print a report, or to work over a list with complex processing for each...
6
552
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance? The following 2 first lines of code - I think - can't raise exceptions: Try Dim str As String Dim intVar As Integer
4
3125
by: zzfreddybb | last post by:
We are using HP aCC compiler on a HP Itanium box ( 11.23) We are having some severe performance hits using exception handling ( try/catch ) scenarios. The online aCC documentation says: HP aC++ exception handling has no significant performance impact at compile-time or run-time. We have not found this to be the case at all.
37
2927
by: clintonG | last post by:
Has somebody written any guidelines regarding how to determine when try-catch blocks should be used and where their use would or could be considered superfluous? <%= Clinton Gallagher METROmilwaukee (sm) "A Regional Information Service" NET csgallagher AT metromilwaukee.com URL http://metromilwaukee.com/ URL...
22
3349
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 why. Is this true? If so, why? Thanks. STom
4
1909
by: tshad | last post by:
Normally, I surround my Dataset/fill or DBreader execut with a try/Catch. Something like: ****************************************************** Dim dbReader As SqlDataReader Dim ConnectionString as String =System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_Connection") Dim objConn as New SqlConnection...
7
10268
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this purpose I set the corresponding XercesDOMParser feature as shown in the upcoming subsection of my code. As far as I understand, the parsing process...
5
4684
by: Nick Keighley | last post by:
Hi, I know in principal this is unanswerable as it is be implementation dependent.I have a code base that is heavily littered with try/catch clauses (the CASE tool has been set up to put a try/catch round every member function). Now I know this is BAD but I just wondered how bad. Will it kill
10
1869
by: Curious | last post by:
Hi, I've found that if I wrap my code with a try-catch block in the fashion illustrated below, it'll do what is specified in the catch section instead of crashing. Could anyone confirm that this is true? If it's true, I'll modify every single method in my program to wrap it with a try-catch block to prevent it from crashing. FYI, the...
83
4133
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about it too? Once I saw a website comparing Prototype to Java and jQuery to Ruby... but now that I read more and more about Prototype, it is said...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.