vba · DOCS

Implementing Try-Catch-Finally in VBA

Maintained on

In general, it is common to perform error handling in each function and then throw to the calling code. However, implementing this in VBA is not straightforward.

If you create an error handler, the error is treated as handled inside that procedure and is not propagated to the caller. To perform cleanup and still notify the caller, you must raise the error again with Err.Raise inside the handler.

A specific example is:

  • When using the Open function to open a file, you may want to ensure that the Close operation still occurs even if an error occurs.
  • Additionally, you want to propagate the error to the calling code.
errNumber = Err.Number
errDescription = Err.Description
On Error GoTo 0
Call Err.Raise(errNumber, , errDescription)

To resolve the issue, you can use code like the above. Let me explain and show you some examples.

On Error GoTo 0 and Saving Error Information

On Error GoTo 0 disables the On Error setting currently active in the procedure. When used inside a handler, any later error is not handled by this procedure and is passed to the caller.

Note that if you raise a new error with Err.Raise while an error handler is running, it does not return to the same handler either; it is passed to the caller. The value of On Error GoTo 0 here is that it makes explicit in the code that this procedure no longer handles errors.

The contents of the Err object can be overwritten or cleared by later processing (such as On Error statements (including On Error GoTo 0), Resume or Exit Sub). It is therefore safest to save Err.Number and Err.Description into variables at the start of the handler. If you pass the description to Err.Raise, the caller can also see the original error message.

The flow is:

  1. An error occurs and execution jumps to the error handler.
  2. Save the error information in variables.
  3. Cancel the handler with On Error GoTo 0.
  4. Perform cleanup (such as Close).
  5. Raise the error to the caller with Err.Raise.

This allows you to replicate Try-Catch-Finally, which includes throwing in the catch block, as seen in other programming languages.

Code Sample

Handling Errors and Throwing Only

Public sub Caller()
	On Error GoTo ERROR_HANDLER
	Call Called

ERROR_HANDLER:
	Debug.Print(Err.Number)
End Sub
Public sub Called()

	On Error GoTo ERROR_HANDLER
	Dim errNumber as Long

		' Write the code where an error may occur

	    Exit Sub

	ERROR_HANDLER:
		errNumber = Err.Number
		On Error GoTo 0

		' Write the code for handling the error here

		' Propagate the error to the caller
	Call Err.Raise(errNumber)

End Sub

Replicating Try-Catch-Finally

It requires quite a lengthy description.

Public sub Caller()

	On Error GoTo ERROR_HANDLER

	Call Called

ERROR_HANDLER:
	Debug.Print(Err.Number)
End Sub

Public sub Called()

	On Error GoTo ERROR_HANDLER
	Dim errNumber as Long

	' Write the code where an error may occur

FINALLY:

	If errNumber <> 0 then
		Call Err.Raise(errNumber)
	End if

	Exit Sub
ERROR_HANDLER:
	errNumber = Err.Number
	On Error GoTo 0

	' Write the code for handling the error here

 	Resume FINALLY
End Sub

Example Sample

When using Try-Catch only

'------------------------------------------------
'
' Function that calls another function
'
'------------------------------------------------
Public Sub Caller()
	On Error GoTo ERROR_HANDLER
	Call Called

ERROR_HANDLER:

	Debug.Print "Caller: Catch"

End Sub
'------------------------------------------------
'
' Function that is called (performs Throw)
'
'------------------------------------------------
Public Sub Called()

	On Error GoTo ERROR_HANDLER
	Dim errNumber as Long

	Dim n As Long: n = 1 / 0

	Exit Sub

ERROR_HANDLER:
	errNumber = Err.Number
	On Error GoTo 0

	Debug.Print "Called: Catch"

	' Propagate the error to the caller
	Call Err.Raise(errNumber)

End Sub

Execution Result

Called: Catch
Caller: Catch

When reproducing Try-Catch-Finally

'------------------------------------------------
'
' Function that calls another function
'
'------------------------------------------------
Public Sub Caller()

	On Error GoTo ERROR_HANDLER

	Call Called

ERROR_HANDLER:

	Debug.Print "Caller: Catch"

End Sub

'------------------------------------------------
'
' Function that is called (performs Throw)
'
'------------------------------------------------
Public Sub Called()

	On Error GoTo ERROR_HANDLER

	Dim errNumber as Long

	Dim n as Long: n = 1 / 0

FINALLY:

	Debug.Print "Called: Finally"

	If errNumber <> 0 then
		Call Err.Raise(errNumber)
	End if

	Exit Sub
ERROR_HANDLER:
	errNumber = Err.Number
	On Error GoTo 0

	Debug.Print "Called: Catch"

 	Resume FINALLY
End Sub

Execution Result

Called: Catch
Called: Finally
Caller: Catch
  • #Excel
  • #VBA