Error catching and throwing - code
If you want a better way of dealing with error the following module may be of use to you. Comments are appreciated. I will provide a sample test in the next post so you can give it a bash.
'Option Compare Database
Option Explicit
'************************************************* **
'AUTHOR: RJ Palmer
'COUNTRY: Australia
'OCCUPATION: Mechatronic engineer
'DATE: 26/8/04
'DESCRIPTION: Ever wanted a better way of handling errors in
' VBA? Well if you like JAVA's exceptions or writing especially
' complex code or temperamental data that you would like to follow all errors
' from there
' source up to the calling level then this might be good for you
'USAGE: Anyone can use or modify this code. A mention of my efforts in a
' lowly comment would be appreciated
'************************************************* ***
Private G_ERROR As MYERR 'the underlying error variables that stores the error
Private Const C_DELIM_SRC = "-->"
Private Const C_DELIM_DESCR = "-->"
Public Enum ERR_type 'my own personal set of error types (add your own)
ERR_NOTFOUND
ERR_arithmetic
ERR_overflow
ERR_incompat
ERR_GENERAL
ERR_none
ERR_sql_exec 'sql execution error
ERR_unexpected
ERR_write
ERR_emptyArray
End Enum
Private Type MYERR 'the underlying error variables type specification
number As ERR_type
descr As String
source As String
isErr As Boolean
End Type
'clears an error flag
Public Function ERR_clear()
err.Clear
With G_ERROR
.descr = ""
.isErr = False
.number = ERR_none
.source = ""
End With
End Function
Public Function ERR_getSysERRNum()
ERR_getSysERRNum = err.number
End Function
'public sub ERR_raise2
Public Sub ERR_raise_blank()
Call err.Raise(0, "", "", 0, 0) 'raise a dummy system error
End Sub
'raises a new error
Public Sub ERR_raise(number As ERR_type, source As String, descr As String)
'Dim ERROR As ErrObject
Static i As Integer
Const C_ERR_RUNTIME = 1
Dim FLAG_throwToFar As Boolean
FLAG_throwToFar = True
G_ERROR.descr = descr
G_ERROR.isErr = True
G_ERROR.source = source
i = i + 1
Call err.Raise(i, "", "", 0, 0) 'raise a dummy system error
End Sub
'Automatically determines whether to throw an error of raise a new one
Public Sub ERR_throw_auto(err As ERR_type, source As String, descr As String)
If ERR_isThrow Then
Call ERR_THROW(source, descr)
'Call ERR_raise(err, source, descr)
Else
Call ERR_raise(err, source, descr)
End If
End Sub
Public Sub ERR_THROW(source As String, descr As String)
Static id As Integer
id = id + 1
Call ERR_raise(G_ERROR.number, G_ERROR.source & C_DELIM_SRC & "(" & id & ")" & source, _
G_ERROR.descr & C_DELIM_DESCR & "(" & id & ")" & descr)
End Sub
Public Function ERR_isThrow() As Boolean
ERR_isThrow = G_ERROR.isErr And Not G_ERROR.number = ERR_none
End Function
Public Function ERR_toString() As String
Dim s As String, number As ERR_type
Const CASE_L = "<": Const CASE_R = ">"
Select Case number
Case ERR_type.ERR_arithmetic
s = "ERR_ARITHMETIC"
Case ERR_type.ERR_GENERAL
s = "ERR_GENERAL"
Case ERR_type.ERR_incompat
s = "ERR_INCOMPATIBLE_TYPES"
Case ERR_type.ERR_NOTFOUND
s = "ERR_NOTFOUND"
Case ERR_type.ERR_overflow
s = "ERR_OVERFLOW"
Case ERR_type.ERR_unexpected
s = "ERR_UNEXPECTED"
Case Else
s = "NOT VALID ERROR" 'default case
End Select
s = CASE_L & s & CASE_R
s = s & " ::SRC::{ " & G_ERROR.source & " } ::DSCR::{ " & G_ERROR.descr & " } "
ERR_toString = s
End Function
Public Sub ERR_msgbox()
MsgBox ERR_toString, vbOKOnly, "EXCEPTION"
End Sub
Public Sub ERR_debug()
Debug.Print "EXCEPTION>>> " & ERR_toString
End Sub
|