Solved

Error handling question

Posted on 2003-12-08
5
292 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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