Wiki Home

Safe Procedures

Namespace: WIN_COM_API
A method of implementing Safe Error Handling.
Definition: SEM Safe Error Message - an error message returned by a Safe Function. The format is:
strSEM = "Routine Name, Error Number, Error Message"
This type of message is returned by all Safe Functions.
There are 3 types of safe procedures: safe functions, defensive functions (used inline), and defensive subroutines (also inline). All safe procedures are
For instance, here is a Safe Function that returns the lower bound of an array:
Function sfArrayLB(ByRef BoundedArray As Variant, _
ByRef LowerBound As Variant) As String '----------------------------------------------- 'Purpose: Pass back the lower bound of an array 'Inputs: BoundedArray is the array to be tested 'Passed: LowerBound is the lower bound of BoundedArray 'Returns: Safe Error Message 'Outputs: None 'Notes: None Usage: strSEM = sfArrayLB(intArray, intLower) '----------------------------------------------- On Error GoTo ErrTrap
' Get lower array bound, could cause an error
' if array has not been instantiated.
LowerBound = LBound(BoundedArray)
sfArrayLB = "" ' Success
Exit Function
sfArrayLB = "sfArrayLB" & vbTab _
& CStr(Err.Number) & vbTab _
& Err.Description
On Error GoTo 0 Exit Function

This is a trivial example, but it illustrates most of the important concepts of the system.
  • You know that when you call this routine you will get valid, useful information back. Either the lower bound or an error message with all the applicable error data.
  • The routine is short and self-describing, with a header that includes a sample use of the routine.
  • If a safe procedure calls another safe procedure, if the inner one raises a Safe Error, the outer one can concatenate it to it's own error so the calling routine will know there is really just one error instead of 2.
  • It's consistant. Imagine a code library made of dozens of routines like this, with the same layout and conventions, in easily searchable shared modules. I'm (just starting to) write a wrapper for Visual Source Safe that will show the headers of each routine in a standard TreeView/ListView format.

Category Error Handling
( Topic last updated: 2000.06.21 01:15:42 AM )