473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Try... Catch performance

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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that
won't help me at all?

TIA,
Erik Cruz
Nov 22 '05 #1
6 2975
No. The expensive thing is raising an exception. While an exception is
not raised, there is not performance penalty. (at least not
considerably)

On Sat, 18 Oct 2003 17:02:03 -0300, "Erik Cruz"
<er******************@antares.com.br> wrote:
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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that
won't help me at all?

TIA,
Erik Cruz


Nov 22 '05 #2
Try...Catch is only needed around code that could throw an exception. So,
if you have lots of code that does very simple work, but only one line that
could throw an exception, you normally would just wrap the one line in the
exception and catch only the exception that you are concerned with.

Exceptions are expensive when thrown, but only when thrown. That is the key
point. Simply having code within a try block does not necessarily make your
code run slower. If you wrap code that never throws an exception in a try
block, it will run just as fast as if there was no try block.

Also, unless you have reason to, you might want to limit your catch
statement to specific exceptions. Catching the base Exception is usually
not done and offers little help when analyzing what happened.

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:#c**************@TK2MSFTNGP11.phx.gbl...
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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that
won't help me at all?

TIA,
Erik Cruz

Nov 22 '05 #3
Those two declarations don't do anything, so they can't throw an
exception..hence no need to wrap them in the try catch block. I haven't
looked at the IL, but I don't believe variable declaration are treated the
same..but I'm not sure, I'll have to get back to you.

In addition to performance, there are good reasons to only trap small bits
of code and in most instances, don't trap System.Exception except in
instances where you know you want to do so. If you had an OutOfMemory
Exception raised in that block, your routine would catch it. However, there
isn't much you are going to do programatically about this problem, and you
want to use exception handlers to respond to things are try to elegantly
recover. In general wrap risky code blocks,like Opening Database
connections or filestreams, where things beyond your code logic can cause
problems. There are other instances where things are very unlikely to occur
if you are careful coding like getting an OutOfRange exception in a loop.
If you carefully and liberally use Assertions in your test environment, you
can responsibly conclude that your code won't throw certain types of
exceptions, but this approach is not to say that you don't want to use
handlers at all.

In VB6 there was this ghastly construct called OnError Resume, and there
were a lot of progrrammers that thought this was all you need for exception
handling. In .NET, such a constrcut will work in VB.NET, but what it does
is wrap every single line of code in the block in it's own try catch
statement. This is awful for performance (not to mention that it can/does
cover up coding flaws). Most articles that I've read about not throwing
unneeded exceptions relate to people carelessly throwing out try catch
blocks, or intentionally raising an exception when an event would be much
more appropriate. For instance, in a BankAccount Class, if the user deducts
more than is avaialable, you could have a OverDrawn event that simply tells
the user this transaction won't work b/c the money isn't there. This is
pretty straightforward OO approach to such a problem. On the other hand,
you could throw a custom exception NotEnoughMoneyException. The problem
with the second approach is that A) You'd need to wrap your deposit function
with a try Catch to catch this, and that this isn't really an exception from
the compiler's point of view.

There are instances where you can just eat an exception, and it's ok to do
so. After all, the whole purpose of structured exceptions is to Handle
problems that you know can come up, and you can't necessarily preepmt with
your code.

If you follow John Robbins (the MSDN BugSlayer - buy his book if you don't
have it already), or Jeffrey Richter to use just two examples, they seldom
have more than a line of code wrapped in a try catch block. At most you
might see three or four but that is rare. Very precise, very targeted.
That is usually what most articles are advocating when they mention this.

HTH,

Bill
"Erik Cruz" <er******************@antares.com.br> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that
won't help me at all?

TIA,
Erik Cruz

Nov 22 '05 #4
> looked at the IL, but I don't believe variable declaration are treated the
same..but I'm not sure, I'll have to get back to you.
This is probably not a realistic case, but will throw an exception ......

Dim a As Int32
a = 45000

Dim b As Int16 = a

Bob Lehmann

"William Ryan" <do********@comcast.nospam.net> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl... Those two declarations don't do anything, so they can't throw an
exception..hence no need to wrap them in the try catch block. I haven't
looked at the IL, but I don't believe variable declaration are treated the
same..but I'm not sure, I'll have to get back to you.

In addition to performance, there are good reasons to only trap small bits
of code and in most instances, don't trap System.Exception except in
instances where you know you want to do so. If you had an OutOfMemory
Exception raised in that block, your routine would catch it. However, there isn't much you are going to do programatically about this problem, and you
want to use exception handlers to respond to things are try to elegantly
recover. In general wrap risky code blocks,like Opening Database
connections or filestreams, where things beyond your code logic can cause
problems. There are other instances where things are very unlikely to occur if you are careful coding like getting an OutOfRange exception in a loop.
If you carefully and liberally use Assertions in your test environment, you can responsibly conclude that your code won't throw certain types of
exceptions, but this approach is not to say that you don't want to use
handlers at all.

In VB6 there was this ghastly construct called OnError Resume, and there
were a lot of progrrammers that thought this was all you need for exception handling. In .NET, such a constrcut will work in VB.NET, but what it does
is wrap every single line of code in the block in it's own try catch
statement. This is awful for performance (not to mention that it can/does
cover up coding flaws). Most articles that I've read about not throwing
unneeded exceptions relate to people carelessly throwing out try catch
blocks, or intentionally raising an exception when an event would be much
more appropriate. For instance, in a BankAccount Class, if the user deducts more than is avaialable, you could have a OverDrawn event that simply tells the user this transaction won't work b/c the money isn't there. This is
pretty straightforward OO approach to such a problem. On the other hand,
you could throw a custom exception NotEnoughMoneyException. The problem
with the second approach is that A) You'd need to wrap your deposit function with a try Catch to catch this, and that this isn't really an exception from the compiler's point of view.

There are instances where you can just eat an exception, and it's ok to do
so. After all, the whole purpose of structured exceptions is to Handle
problems that you know can come up, and you can't necessarily preepmt with
your code.

If you follow John Robbins (the MSDN BugSlayer - buy his book if you don't
have it already), or Jeffrey Richter to use just two examples, they seldom
have more than a line of code wrapped in a try catch block. At most you
might see three or four but that is rare. Very precise, very targeted.
That is usually what most articles are advocating when they mention this.

HTH,

Bill
"Erik Cruz" <er******************@antares.com.br> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that won't help me at all?

TIA,
Erik Cruz


Nov 22 '05 #5
Point taken, I should have been more clear. An initialization coupled with
a Declaration could possibly throw an exception, but I don't think a simple
declaration could.
"Bob Lehmann" <no****@dontbotherme.zzz> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
looked at the IL, but I don't believe variable declaration are treated the
same..but I'm not sure, I'll have to get back to you.


This is probably not a realistic case, but will throw an exception ......

Dim a As Int32
a = 45000

Dim b As Int16 = a

Bob Lehmann

"William Ryan" <do********@comcast.nospam.net> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Those two declarations don't do anything, so they can't throw an
exception..hence no need to wrap them in the try catch block. I haven't
looked at the IL, but I don't believe variable declaration are treated the same..but I'm not sure, I'll have to get back to you.

In addition to performance, there are good reasons to only trap small bits of code and in most instances, don't trap System.Exception except in
instances where you know you want to do so. If you had an OutOfMemory
Exception raised in that block, your routine would catch it. However,

there
isn't much you are going to do programatically about this problem, and you want to use exception handlers to respond to things are try to elegantly
recover. In general wrap risky code blocks,like Opening Database
connections or filestreams, where things beyond your code logic can cause problems. There are other instances where things are very unlikely to

occur
if you are careful coding like getting an OutOfRange exception in a loop. If you carefully and liberally use Assertions in your test environment,

you
can responsibly conclude that your code won't throw certain types of
exceptions, but this approach is not to say that you don't want to use
handlers at all.

In VB6 there was this ghastly construct called OnError Resume, and there
were a lot of progrrammers that thought this was all you need for

exception
handling. In .NET, such a constrcut will work in VB.NET, but what it does is wrap every single line of code in the block in it's own try catch
statement. This is awful for performance (not to mention that it can/does cover up coding flaws). Most articles that I've read about not throwing
unneeded exceptions relate to people carelessly throwing out try catch
blocks, or intentionally raising an exception when an event would be much more appropriate. For instance, in a BankAccount Class, if the user

deducts
more than is avaialable, you could have a OverDrawn event that simply

tells
the user this transaction won't work b/c the money isn't there. This is
pretty straightforward OO approach to such a problem. On the other hand, you could throw a custom exception NotEnoughMoneyException. The problem
with the second approach is that A) You'd need to wrap your deposit

function
with a try Catch to catch this, and that this isn't really an exception

from
the compiler's point of view.

There are instances where you can just eat an exception, and it's ok to do so. After all, the whole purpose of structured exceptions is to Handle
problems that you know can come up, and you can't necessarily preepmt with your code.

If you follow John Robbins (the MSDN BugSlayer - buy his book if you don't have it already), or Jeffrey Richter to use just two examples, they seldom have more than a line of code wrapped in a try catch block. At most you
might see three or four but that is rare. Very precise, very targeted.
That is usually what most articles are advocating when they mention this.
HTH,

Bill
"Erik Cruz" <er******************@antares.com.br> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
'More code here

Catch Ex As Exception

End Try

Should I remove the variable declarations from the Try block or doing that won't help me at all?

TIA,
Erik Cruz



Nov 22 '05 #6
All declarations will be for the method in the IL anyways. A declaration
doesn't do anything except make IL emit the .locals(whatever, whatever,
etc.).
-mike
MVP

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
Point taken, I should have been more clear. An initialization coupled with a Declaration could possibly throw an exception, but I don't think a simple declaration could.
"Bob Lehmann" <no****@dontbotherme.zzz> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
looked at the IL, but I don't believe variable declaration are treated the same..but I'm not sure, I'll have to get back to you.
This is probably not a realistic case, but will throw an exception .......

Dim a As Int32
a = 45000

Dim b As Int16 = a

Bob Lehmann

"William Ryan" <do********@comcast.nospam.net> wrote in message
news:OO**************@TK2MSFTNGP12.phx.gbl...
Those two declarations don't do anything, so they can't throw an
exception..hence no need to wrap them in the try catch block. I haven't looked at the IL, but I don't believe variable declaration are treated the same..but I'm not sure, I'll have to get back to you.

In addition to performance, there are good reasons to only trap small bits of code and in most instances, don't trap System.Exception except in
instances where you know you want to do so. If you had an OutOfMemory
Exception raised in that block, your routine would catch it. However,

there
isn't much you are going to do programatically about this problem, and you want to use exception handlers to respond to things are try to elegantly recover. In general wrap risky code blocks,like Opening Database
connections or filestreams, where things beyond your code logic can cause problems. There are other instances where things are very unlikely to occur
if you are careful coding like getting an OutOfRange exception in a loop. If you carefully and liberally use Assertions in your test
environment,
you
can responsibly conclude that your code won't throw certain types of
exceptions, but this approach is not to say that you don't want to use
handlers at all.

In VB6 there was this ghastly construct called OnError Resume, and
there were a lot of progrrammers that thought this was all you need for

exception
handling. In .NET, such a constrcut will work in VB.NET, but what it

does is wrap every single line of code in the block in it's own try catch
statement. This is awful for performance (not to mention that it can/does cover up coding flaws). Most articles that I've read about not throwing unneeded exceptions relate to people carelessly throwing out try catch
blocks, or intentionally raising an exception when an event would be much more appropriate. For instance, in a BankAccount Class, if the user

deducts
more than is avaialable, you could have a OverDrawn event that simply

tells
the user this transaction won't work b/c the money isn't there. This is pretty straightforward OO approach to such a problem. On the other hand, you could throw a custom exception NotEnoughMoneyException. The problem with the second approach is that A) You'd need to wrap your deposit

function
with a try Catch to catch this, and that this isn't really an exception from
the compiler's point of view.

There are instances where you can just eat an exception, and it's ok
to do so. After all, the whole purpose of structured exceptions is to
Handle problems that you know can come up, and you can't necessarily preepmt with your code.

If you follow John Robbins (the MSDN BugSlayer - buy his book if you don't have it already), or Jeffrey Richter to use just two examples, they seldom have more than a line of code wrapped in a try catch block. At most you might see three or four but that is rare. Very precise, very targeted. That is usually what most articles are advocating when they mention this.
HTH,

Bill
"Erik Cruz" <er******************@antares.com.br> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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
> 'More code here
>
> Catch Ex As Exception
>
> End Try
>
> Should I remove the variable declarations from the Try block or

doing that
> won't help me at all?
>
> TIA,
> Erik Cruz
>
>



Nov 22 '05 #7

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

Similar topics

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?...
11
3460
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) {}
7
5386
by: BC | last post by:
Hi all, I have a method that accepts a string as the parameter. This can be a base64 image string, or just a normal string. Currently to test whether it's a image string or just a normal string,...
37
2913
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...
22
3342
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...
7
1362
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
9
3356
by: Michael MacDonald | last post by:
Does someone have a good site I can visit or explain the use of Try" and Catch foe exception/error handling. What is the logic behind this command and maybe an example would be great!!!! Mike_Mac...
4
1904
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...
5
4679
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...
9
3327
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
7223
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
7115
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
7377
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...
1
7036
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
7489
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
5624
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,...
1
5047
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...
0
3191
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...
0
414
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...

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.