Error Trapping

  • Thread starter Thread starter JK
  • Start date Start date
J

JK

For many of my procedures I trap errors with On Error GoTo Handler. Would it
be best to add Resume Next to the Handler?

Sub ()
On Error GoTo Handler
Code:
Exit Sub

Handler:
[whatever]
Resume Next
End Sub

Or is there a better approach? Thank you in advance.

Jim Kobzeff
 
It depends what you want to happen when an error occurs. If your
error handling code can fix the error, use Resume or Resume Next
to resume execution in the main section of the code. If your
error handling code can't fix the error, you probably don't want
to resume, instead exit the sub.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
That depends entirely on the nature of the error. If the error is critical
and there is no point in going onn after the error then resuming next would
not cut it. For example if the code needs to read some data from another
file, if that file can not be found, it may not make sense to continue on
with the code. On the other hand, if it can not find the file but you can
reasonably expect the end user to be able to find the file, then your error
handler may just show a find file dialog. Once the file has been located then
continue. If the user can not find the file then you still would just end the
process.

There are as many correct ways to handle an error as there are possible
errors.
 
Back
Top