exception - Good Patterns For VBA Error Handling -


Are there some good patterns to deal with error in VBA?

Specifically, what I should do is this situation:

  ... some code ... ... some code where an error may occur ... ... some code ... ... some other code where a different error could occur ... ... some other code ... ... some code that should always be run (like the end block). ..  

I want to handle both errors, and resume the execution of the error after the code can occur. Also, at the end, the code should always run at the end - before an exception has been thrown before. How can I get this result?

error handling in VBA


  • Get on error error handler label
  • resume ( next | error handler label )
  • error goto 0 (current error handler disabled)
  • er Object

Resets the property properties normally to zero or zero-strings, which is in the error handling routine, but this Psht the error. Clear .

Error handling routine ending errors.

Category 513-65535 User Errors are available for custom class errors, you add vbObjectError to an error number.

For the interface members implemented in an derived class, you must continuously E_NOTIMPL = & amp; H80004001 .


  option clear sub-handleError () delete an integer error as error GoTo errMyErrorHandler a = 7/0 error on GoTo 0 debug. Print "This line will not be executed." DoCleanUp: a = 0 Exit sub errMyErrorHandler: MsgBox Err.Description, _vbExclamation + vbOKCancel, _ "Error:" & amp; Resume CSTR (error number) DoCleanUp End Sub Sub RaiseAndHandleError () error GoTo errorMyErrorHandler is available for 'Category 513-65535 User Errors. 'For class errors, you add vbObjectError to error number. Err.Raise vbObjectError + 513, "Module 1 :: Test ()", "My custom error." Error on GoTo 0 debug Print "This line will be executed." Exit All MarrierEirhardler: MSBBC Error Description, _ vbExclamation + vbOKCancel, _ "Error:" & amp; CStr (Err.Number) Err.Clear restart Next End Sub Sub FailInErrorHandler () Delete an error as an integer in the form of an error err IrrorHandler a = 7/0 Error GoTo 0 debug.Print "This The line will not be executed. " DoCleanUp: a = 0 exit sub errMyErrorHandler: a = 7/0 '& lt; == Terminal error! MsgBox Err.Description, _ vbExclamation + vbOKCancel, _ "Error:" & amp; Resume CSTR (Error Number) DoCleanUp End Sub Sub DontDoThis () 'No error will be noticed! Restart the error 'some complex code which fails here and start over all Sub DoThisIfYouMust () error Next' There are some code that may fail but you do not care error GoTo 0 'More code here Sub  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -