ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Trapping (https://www.excelbanter.com/excel-programming/354794-error-trapping.html)

JK

Error Trapping
 
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



Chip Pearson

Error Trapping
 
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


"JK" wrote in message
news:HhpNf.2710$DT.491@trnddc06...
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




Jim Thomlinson[_5_]

Error Trapping
 
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.
--
HTH...

Jim Thomlinson


"JK" wrote:

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





All times are GMT +1. The time now is 09:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com