Good error checking technique?
Hey all,
I consider myself an advanced programmer - my one downfall being error checking. I know it's a pretty important topic - so I'm looking to improve. I use error checking often, but not all the time. If I see the potential for a problem I'll typically throw an on error goto err statement in and a label at the end of the sub with a messagebox specifying error number and name. That's not always sufficient though.
I recently saw a forum talking about using the on error resume next statement and then checking the err object in the following statement for problems. I also am interested in getting a line numbering add on and specifying the line number in the message box.
The problem with the second method is that I'm really not handling any errors, just passing the information on to the user. I need some good techniques for handling errors. I don't want to go too crazy anticipating every error (such as a data type conversion error), but I want to make sure I'm thorough.
If I get some good responses, I'd recommend stickying this thread - it'd be very useful for everyone.
Re: Good error checking technique?
There are a few articles about error handling in our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page), including one which shows how to create an error handler, with simple examples of how it can 'fix' expected problems.
In terms of adding line numbers etc, see this post, which explains the way I do it (using the MZTools addin).
Even when you aren't expecting errors to occur in a routine, it is a good idea to add an error handler to it anyway, as most errors are unexpected!
If you use MZTools this is extremely simple - as you just need to go into a routine, and press a button.
Re: Good error checking technique?
That's a great post and helped a lot, but I'm looking for something a bit more in depth. For example, in what cases to use which checking statements, what to check for etc...or does MZTools do that automatically?
ADDENDUM: After looking at MZTools and liking at fater about 5 minutes, I noticed it handles a good deal of most errors - my only question is, is there a way to customize the error handler it creates without doing it manually. I wnat to be able to click the add error handler button and get a message that specifies line number also without having to add it in afterwards.
Re: Good error checking technique?
I'm not sure what you meant there.. are you talking about what to do to "auto-fix" expected errors? If so, that is something which is extremely specific to each routine and/or error, so needs to be specifically written for it.
MZTools does not do any "real" work for you in the error handler, what it basically does is paste (and fill in) a template that you created. If you use Erl in your template (or better, in a routine that your template calls) then the line numbering feature of MZTools is amazing in terms of the time it saves.
Re: Good error checking technique?
note that some errors can never be solved how eva much u try, i encoutered 1 when there were icon file which were currupt, the program could not read it, so its easier to type "on error resume next" cos u cant solve that.
Re: Good error checking technique?
Right. Si how do you change the template the MZTools looks for? I'm aware that it doesn't do any handling and cuts and pastes, but on complicated routines i'd be adding an error handler and then line numbers, so I wanted to use Erl in the template instead of manually typing it in each time, but in order to do that I need to know where that template is.
Re: Good error checking technique?
nevermind I'm retarded and figured it out.
Thanks for your help Si. MZTools is officially awesome.
Re: Good error checking technique?
To change the template, go to the options of MZTools (via the icon on the toolbar, or via "Tools"->"MZTools"->"Options"), and click on the "Error Handler" tab. edit: I was too slow!
I'd recommend creating a sub to do the actual error handling (as shown in my example template), as it then allows you to add extra functionality later if you want (without having to change lots of existing code). If you want to deal with special cases, you would do that before/instead of the call to the sub.
Quote:
Originally Posted by Eranga Pilimatalawwe
note that some errors can never be solved how eva much u try, i encoutered 1 when there were icon file which were currupt, the program could not read it, so its easier to type "on error resume next" cos u cant solve that.
Just because an error cannot be solved, it doesn't mean that you should ignore it (as you implied, but might not have meant).
Generally it is best to deal with it in a way that is appropriate (such as showing a message like "file not valid" or "error XX occurred").
Re: Good error checking technique?
Anyone else have any trouble with MZTools causing the IDE to act up?
Such things as when you go to load a ocx or reference it doesn't work on the first try. Also IDE crashing? (VB5)
Mike
Re: Good error checking technique?
Quote:
Originally Posted by MikeJoel
Anyone else have any trouble with MZTools causing the IDE to act up?
Such things as when you go to load a ocx or reference it doesn't work on the first try. Also IDE crashing? (VB5)
Mike
I haven't had any problems with MZTools specifically. When I try to add an OCX I get the message "Error reading from the registry," but I attribute that to the fact that I'm running Vista. How do you know it's MZTools?
Re: Good error checking technique?
I've been using it for many years (on several computers) and never had those issues - the only one I've ever had is that it can clear the clipboard when you open VB.
If it is actually MZTools which is causing the problem, it may be because you didn't install it correctly (note that you need the VB6 runtimes), or because of some other issue which they have documented in their FAQs.
Re: Good error checking technique?
Quote:
Originally Posted by drag0n_45
I haven't had any problems with MZTools specifically. When I try to add an OCX I get the message "Error reading from the registry," but I attribute that to the fact that I'm running Vista. How do you know it's MZTools?
I think it's a "Vista thing", I've had a similar message when adding a reference - it still added the reference - and I dont use MZTools, or have it installed.
Re: Good error checking technique?
Quote:
Originally Posted by Doogle
I think it's a "Vista thing", I've had a similar message when adding a reference - it still added the reference - and I dont use MZTools, or have it installed.
Yeah. You have to go through it twice or sometimes three times to get it to add though.