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

Home Posts Topics Members FAQ

Where to start the try block and what's the overhead???

Hello,

this is something I've been asking myself for sometime ... and now I'd like
to clarify this point.
When should the try block start in a method??? What I mean is, does having
all the code instead of a smaller set of it inside a try clause add any
overhead??? 1What I'd like to understand is if, to be completely sure that
no unhandles exception will get to the user, I can place all the code inside
a try block
and if this practice adds unnecessary overhead (memory usage, more cpu
cycles, etc.) or if having a smaller set of instructions instead of all the
code under a try has the EXACT SAME effect on resources (overhead).
Bob Rock

Nov 16 '05 #1
3 2028
try blocks add overhead when they are fired or spring into action. Otherwise
they don't. The issue with wrapping a big nasty try block is that you aren't
really able to tell what entity fired the exception.

The implicit assumption in a try block is that you are prepared to handle a
particular type
of exception. A big all encompassing try block defeats that purpose since
your code cannot possibly be prepared to handle any and all exceptions. In
fact, I am a proponent of letting certain exceptions
blow the application apart - a stack overflow or memory exhaustion exception
are good examples of that.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Bob Rock" <no************ *************** @hotmail.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hello,

this is something I've been asking myself for sometime ... and now I'd
like
to clarify this point.
When should the try block start in a method??? What I mean is, does having
all the code instead of a smaller set of it inside a try clause add any
overhead??? 1What I'd like to understand is if, to be completely sure that
no unhandles exception will get to the user, I can place all the code
inside
a try block
and if this practice adds unnecessary overhead (memory usage, more cpu
cycles, etc.) or if having a smaller set of instructions instead of all
the
code under a try has the EXACT SAME effect on resources (overhead).
Bob Rock

Nov 16 '05 #2
Alvin is pointing out a very important design principle. Exception handling is
meant
for two purposes, reporting and recovery. You should handle exceptions if you
need
to report them, and only then crash out or exit the app gracefully. You should
also
handle exceptions if you think you can recover from them.

Note that in the case of a stack overflow or out of memory exception you may not
be
able to gracefully shut-down your application. The best you can do is an
Environment.Exi t
with some error code in the instance your code is being automated.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
try blocks add overhead when they are fired or spring into action. Otherwise
they don't. The issue with wrapping a big nasty try block is that you aren't
really able to tell what entity fired the exception.

The implicit assumption in a try block is that you are prepared to handle a
particular type
of exception. A big all encompassing try block defeats that purpose since
your code cannot possibly be prepared to handle any and all exceptions. In
fact, I am a proponent of letting certain exceptions
blow the application apart - a stack overflow or memory exhaustion exception
are good examples of that.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
"Bob Rock" <no************ *************** @hotmail.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hello,

this is something I've been asking myself for sometime ... and now I'd
like
to clarify this point.
When should the try block start in a method??? What I mean is, does having
all the code instead of a smaller set of it inside a try clause add any
overhead??? 1What I'd like to understand is if, to be completely sure that
no unhandles exception will get to the user, I can place all the code
inside
a try block
and if this practice adds unnecessary overhead (memory usage, more cpu
cycles, etc.) or if having a smaller set of instructions instead of all
the
code under a try has the EXACT SAME effect on resources (overhead).
Bob Rock


Nov 16 '05 #3
Bob,
In addition to the other's comments.

Normally I only include Try/Catch blocks in methods, when I know there is
something specific that I need to do with the exception. For example the
method may cause an UnauthorizedAcc essException, and the
UnauthorizedAcc essException is O.K. and I should just continue processing.

More likely is I will have Try/Finally blocks in methods to clean up
resources. In C# the using statement makes this easier. For example if I
opened a connection to a database, I would use a Try/Finally to ensure the
connection is closed when the method exits.

For most exceptions I simply let the global exception handler deal with the
exception, which normally includes logging the exception, and possible email
notification.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
System.Web.Http Application.Err or event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomai n.UnhandledExce ption event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows. Forms.Applicati on.ThreadExcept ion event
Use AddHandler in your Sub Main.

It can be beneficial to combine the above global handlers in your app, as
well as wrap your Sub Main in a try catch itself.

There is an article in the June 2004 MSDN Magazine that shows how to
implement the global exception handling in .NET that explains why & when you
use multiple of the above handlers...

http://msdn.microsoft.com/msdnmag/is...6/default.aspx

Unfortunately the article is not on-line yet.

Hope this helps
Jay
"Bob Rock" <no************ *************** @hotmail.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hello,

this is something I've been asking myself for sometime ... and now I'd like to clarify this point.
When should the try block start in a method??? What I mean is, does having
all the code instead of a smaller set of it inside a try clause add any
overhead??? 1What I'd like to understand is if, to be completely sure that
no unhandles exception will get to the user, I can place all the code inside a try block
and if this practice adds unnecessary overhead (memory usage, more cpu
cycles, etc.) or if having a smaller set of instructions instead of all the code under a try has the EXACT SAME effect on resources (overhead).
Bob Rock

Nov 16 '05 #4

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

Similar topics

2
1174
by: Bob Rock | last post by:
Hello, this is something I've been asking myself for sometime ... and now I'd like to clarify this point. When should the try block start in a method??? What I mean is, does having all the code instead of a smaller set of it inside a try clause add any overhead??? What I'd like to understand is if, to be completely sure that no unhandles exception will get to the user, I can place all the code inside a try block and if this practice...
17
5053
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
5
4806
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e. "payload") using pointers. Examples of these are binary trees, hash tables etc. Thus, the message itself contains only a pointer to the actual data. When the message is sent to the same processor, these pointers point to the original locations, which...
7
1873
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long as 2300 ms to complete. This is fairly rare, but the test code below will definitely show it. Somehow, I must not have my design right. The idea of this code is that I do two different types of processing ( 1-starting and 2-ending) based on...
2
1934
by: msnews.microsoft.com | last post by:
Hello, I have the scenario. I m building an application either in asp.net or window application. This application is base on n-tier application model. Let us take example of Northwind Database in SQL SERVER. i build an asp.net page. On that page i place the grid with one template column. In that column i put HTML Table with two rows. In First row there are one combobox and two textboxes. In the second row there is another DataGrid means...
37
8910
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here is code used for measuring:
5
7467
by: Tiglath | last post by:
We are building a high performance system and suddenly the cost of using exception has been magnified. What is the actual cost of having a frequent call inside a try-catch block when the vast majority of times there will be no exception to catch? Many thanks.
14
3033
by: karthikbalaguru | last post by:
Hi, In the case of heap , to keep track of a single chunk of memory it requires 8 bytes of information. That is, it requires 4 bytes to hold the size, and 4 bytes to hold the pointer to the next block of memory. So, For every additional chunk, even if it is only one byte long, these 8 bytes are required again, in addition to the 1 byte actually needed to store the chunk itself. So, there should be wastage of memory for managing the...
0
9666
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10145
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5422
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4094
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
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.