Solved

Error handling question

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 63
VBA color chart bars 12 66
Problem to skip loop 6 49
How to measure sizes and angles in scanned images ? 3 27
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now