Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rethrow error in VBA

Tags:

excel

vba

When there's an error I would like to do some cleanup and then let the error flow up so that it's handled globally.

In C# I would do a try finally but that is not possible with VBA, only On Eror GoTo <mylabel>.

How can I rethrow the error properly when caught in the label?

Is it only possible calling Err.Raise again with all properties, no throw like keyword exists?:

Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
like image 563
Kapé Avatar asked Oct 25 '16 21:10

Kapé


1 Answers

That is the correct method, but no, you don't have to re-state all the err properties. The values are retained:

err.raise Err.Number

is the same as

Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/raise-method

It can be more complex if you are throwing out of a class module. By default, the err object of a class module is caught and re-created, so depending on how it is declared, properties may be replaced.

like image 55
david Avatar answered Oct 10 '22 00:10

david