Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Subroutine vs GOTO "On Error"

You are absolutely correct in your assesment of Goto. But like anything else
it is important to understand exactly what it does and when it is
approporiate to use. Goto should be used with error handling and no where
else. Period. Taken to its extreme all procedures should only have one flow
of execution (top to bottom). They should only have one entrance point and
one exit point. This rule is also broken in error handling when we use exit
sub to avoid entering the error handling code at the end of a procedure. The
fact that you know this will help you to write better code. The cardinal
rules (the world according to me) are...

Always use Option Explicit
Avoid Public declarations of subs functions and variables wherever feasable
Avoid Exit Sub and End except at the end of your subs (functions)
Always use error handlers
Don't use error handler for errors you can reasonable anticipate (write code
to handle the problem)
Always document your code
--
HTH...

Jim Thomlinson


"Damian Carrillo" wrote:

This may have been covered previously, but I've not been able to find it on
the boards. In school I was taught that GOTO statements were evil, bad, and
generally not nice. I later learned firsthand about the nightmare of fixing
someone else's GOTO riddled code. Even the VBA help files warn repeatedly
that GOTO use is something to be avoided.

Despite this, the only way I seem to be able to trap errors is by using the
"On Error" statement/method:

'code here
ON ERROR RESUME NEXT
'or
ON ERROR GOTO StupidBuggyCrap
'more code here followed by blocks of code called by GOTO statements
EXIT SUB
Stupidbuggycrap:
'code to trap, display and handle errors
END SUB

I have tried to create separate error subroutines that can be called from
any module in my code, but they always fail to compile, which suggests that
calls to other subroutines are not allowed when using "ON ERROR".

My question: Why is this? And is there a way around this limitation? Am I
overlooking something obvious that allows calls to code in another module to
be used? Or are bad programming habits that make code impenetrable to
outsiders a prerequisite for any error trapping efforts? Will these forced
bad coding habits force me to GOTO hell!?

Damian Carrillo


----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/comm...el.programming

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW IS COLUMN "K" REFERENCED ON THE CURRENT ROW IN THE GOTO COMMAN mbnspect Excel Discussion (Misc queries) 5 January 10th 08 11:50 PM
Question on "On Error GoTo skip" dan Excel Discussion (Misc queries) 2 July 1st 07 10:48 PM
Subroutine vs GOTO "On Error" K Dales[_2_] Excel Programming 0 July 14th 05 09:21 PM
Getting "compile error" "method or data member not found" on reinstall Bp Excel Programming 1 April 23rd 04 04:42 PM


All times are GMT +1. The time now is 02:41 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"