VB6中的ErrorHelper

今天还是修改原先VB6处理的程序,在错误处理方面需要一些改进,弄了一个ErrorHelper的类,还是有点用处的,存到这里吧.

None.gif Option   Explicit
None.gif
None.gif
Private  m_Continue  As   Boolean
None.gif
None.gif
Private  m_MessageString  As   String
None.gif
None.gif
Private  m_DisplayDetailErrInfo  As   Boolean
None.gif
None.gif
Public   Event  onError()
None.gif
None.gif
' 解析错误对象
None.gif'
DefaultMessageString:显示的提示消息,如果为空则显示缺省消息
None.gif'
frm:处理卸载窗体,可选
ExpandedBlockStart.gifContractedBlock.gif
Public   Function Parse() Function Parse(Optional DefaultMessageString As StringOptional frm As Form)
InBlock.gif    
Select Case Err.Number
InBlock.gif        
Case 0
InBlock.gif            m_Continue 
= False
InBlock.gif        
Case Else
InBlock.gif            
If IsMissing(DefaultMessageString) Or Len(DefaultMessageString) = 0 Then
InBlock.gif                
If m_DisplayDetailErrInfo Then
InBlock.gif                    
MsgBox MergeMessage(DefaultMessage), vbCritical, "提示"
InBlock.gif
                Else
InBlock.gif                    
MsgBox DefaultMessage, vbCritical, "提示"
InBlock.gif
                End If
InBlock.gif            
Else
InBlock.gif                
If m_DisplayDetailErrInfo Then
InBlock.gif                    
MsgBox MergeMessage(DefaultMessageString), vbCritical, "提示"
InBlock.gif
                Else
InBlock.gif                    
MsgBox DefaultMessageString, vbCritical, "提示"
InBlock.gif
                End If
InBlock.gif            
End If
InBlock.gif            
If Not IsMissing(frm) Then
InBlock.gif                ExitForm frm
InBlock.gif            
End If
InBlock.gif            m_Continue 
= True
InBlock.gif            
RaiseEvent onError
InBlock.gif    
End Select
InBlock.gif    Err.Clear
ExpandedBlockEnd.gif
End Function

None.gif
None.gif
' 处理完错误后是否进行其他处理
ExpandedBlockStart.gifContractedBlock.gif
Public   Property Get() Property Get Continue() As Boolean
InBlock.gif    Continue 
= m_Continue
ExpandedBlockEnd.gif
End Property

None.gif
None.gif
' 缺省消息
ExpandedBlockStart.gifContractedBlock.gif
Public   Property Get() Property Get DefaultMessage() As String
InBlock.gif    DefaultMessage 
= m_MessageString
ExpandedBlockEnd.gif
End Property

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Property Let() Property Let DefaultMessage(ByVal MessageString As String)
InBlock.gif    m_MessageString 
= MessageString
ExpandedBlockEnd.gif
End Property

None.gif
None.gif
' 卸载窗口
ExpandedBlockStart.gifContractedBlock.gif
Public   Sub ExitForm() Sub ExitForm(frm As Form)
InBlock.gif    
If Not frm Is Nothing Then Unload frm
ExpandedBlockEnd.gif
End Sub

None.gif
None.gif
' 是否显示错误消息
ExpandedBlockStart.gifContractedBlock.gif
Public   Property Get() Property Get DisplayDetailErrInfo() As Boolean
InBlock.gif    DisplayDetailErrInfo 
= m_DisplayDetailErrInfo
ExpandedBlockEnd.gif
End Property

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Property Let() Property Let DisplayDetailErrInfo(ByVal Display As Boolean)
InBlock.gif    m_DisplayDetailErrInfo 
= Display
ExpandedBlockEnd.gif
End Property

None.gif
None.gif
' 合并消息
ExpandedBlockStart.gifContractedBlock.gif
Private   Function MergeMessage() Function MergeMessage(Message As StringAs String
InBlock.gif    MergeMessage 
= MergeString("消息:" & Message, vbCrLf, "编号:", Err.Number, vbCrLf, "说明:", Err.Description)
ExpandedBlockEnd.gif
End Function

None.gif
None.gif
' 合并字符串
ExpandedBlockStart.gifContractedBlock.gif
Private   Function MergeString() Function MergeString(ParamArray arg()) As String
InBlock.gif    
Dim i As Integer
InBlock.gif    
For i = 0 To UBound(arg())
InBlock.gif        MergeString 
= MergeString & arg(i)
InBlock.gif    
Next
ExpandedBlockEnd.gif
End Function

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Private   Sub Class_Initialize() Sub Class_Initialize()
InBlock.gif    
Me.DefaultMessage = "数据产生冲突,请重新进入该功能."
InBlock.gif
    Me.DisplayDetailErrInfo = False
ExpandedBlockEnd.gif
End Sub

None.gif
None.gif
' 退出整个系统
ExpandedBlockStart.gifContractedBlock.gif
Public   Sub ExitSystem() Sub ExitSystem()
InBlock.gif    
MsgBox "产生致命错误,系统即将关闭.", vbCritical, "提示"
InBlock.gif
    End
ExpandedBlockEnd.gif
End Sub

测试代码:
None.gif Dim   WithEvents  eh  As  ErrorHelper
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Private   Sub Command1_Click() Sub Command1_Click()
InBlock.gif#
If ErrorOnOff = 0 Then
InBlock.gif    
On Error GoTo onErrors
InBlock.gif#
End If
InBlock.gif    Err.Raise 
100
InBlock.gif    
MsgBox "OK"
InBlock.gif
onErrors:
InBlock.gif    eh.Parse
InBlock.gif    
'If eh.Continue Then eh.ExitSystem
InBlock.gif
    'If eh.Continue Then Resume Next
ExpandedBlockEnd.gif
End Sub

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Private   Sub eh_onError() Sub eh_onError()
InBlock.gif    Unload 
Me
ExpandedBlockEnd.gif
End Sub

None.gif
ExpandedBlockStart.gifContractedBlock.gif
Private   Sub Form_Load() Sub Form_Load()
InBlock.gif    
Set eh = New ErrorHelper
ExpandedBlockEnd.gif
End Sub

None.gif

通过这些代码可以节约一些重复代码的数量,作为一个小的底层错误处理机制应该还可以.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值