472,796 Members | 1,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1954
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****************@tk2msftngp13.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.Exit
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****************@tk2msftngp13.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****************@tk2msftngp13.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 UnauthorizedAccessException, and the
UnauthorizedAccessException 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.HttpApplication.Error event
Normally placed in your Global.asax file.

For console applications look at:
System.AppDomain.UnhandledException event
Use AddHandler in your Sub Main.

For Windows Forms look at:
System.Windows.Forms.Application.ThreadException 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****************@tk2msftngp13.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
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...
17
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
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....
7
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...
2
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...
37
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...
5
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...
14
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.