Solved

Error handling question

Posted on 2003-12-08
5
291 Views
Last Modified: 2010-05-03
When running my code through several code analyzers, they all say that an error handling statement should be included in the sub or function where I didn't have an 'On Error...' statement.  I didn't think to put error handling in a sub/function where there's only 1 line, e.g. a variable assignment.

Is it advisable to put the 'On Error Resume Next' statement in routines that don't really need error handling?  It seems like there's a lot of code bloat if one does that.

Thanks.
0
Comment
Question by:halfondj
5 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 100 total points
ID: 9896173
Hi halfondj,

As with all things, it is a matter of judgement. Technically I suppose you could say that even a simple variable assignment may generate an error if for example you try to assign too large a value to an integer you will get an overflow error. The issue with this is that if you have error handling in a procedure which then calls another one without error handling, you will not be able to trap the real error because it will get consumed into the calling procedure's error handler.

A rule of thumb is that if anything could cause any error to be raised you need to handle it. There are some VB add-ins which allow you to add such error handling with a single keystroke combination and this is what I tend to use. I have  default error handling script and apply it to any new function or procedure. This way at least I can be sure that I have trapped any errors where they have occurred and not had them consumed by other error handling which can make debugging code quite difficult.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 14

Assisted Solution

by:ajexpert
ajexpert earned 100 total points
ID: 9896189
Hi,
There is no need for declaring error handler in procedure which contains only declaration of variables.  But the general routine should be to add error handlers
These links should you
http://www.vbcity.com/forums/faq.asp?fid=6&cat=Error%20Handling
http://www.vb-helper.com/tut6.htm

0
 
LVL 5

Accepted Solution

by:
mccainz2 earned 100 total points
ID: 9896215
YES YES YES

Error handling in every sub,function etc.....
Much preferable than a runtime crash to desktop
:)

As far as On error resume next ,
I do use that , usually in functions or subs where I'm fairly positive errors wont cascade  as a result ....
 ... also a good idea to mix and match error_handling ...

function start

on error resume next
.....simple code
on error goto err_handler
.....Some ADO code
on error resume next
....more trivial code
err_exit:
exit func
err_handler:
 handle error

function end
0
 

Author Comment

by:halfondj
ID: 9896264
Thanks for all the answers.  I increased the points and split them evenly.
0
 

Author Comment

by:halfondj
ID: 9896265
Thanks for all the answers.  I increased the points and split them evenly.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question