自定义错误号/消息提示 [英] Custom error numbers/messages tip

查看:58
本文介绍了自定义错误号/消息提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您曾在自己的程序中使用过自定义错误编号和消息,那么您可能最终得到的代码类似于我在

过去的事情......


< code>

public Const xyzcErrNumBase As Long = 500

Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1

Public Const xyzcErrTxtHeyDontDoThat ="嘿!不要这样做!"


< ...>


Err.Raise xyzcErrNumHeyDontDoThat ,, xyzcErrTxtHeyDontDoThat


< ...>


< / code>

此处存在重复问题。每次你想要提高

错误时,你必须传递两个常量,并且没有任何强制执行

的事实,你传递的常数是那些一起走。犯了一个错误很容易

(我已经制造了它们),这个错误很可能会被发现,直到有了你之后才会被忽视在某个时候,在某个地方寻找错误的错误时,你会在脑子里摸不着头几个小时。


VB / VBA并没有提供太多的帮助这方面的帮助,如数组常量,

等,但我想出了一个聪明的方法来清理一些东西。将

数和描述合并为一个字符串,然后编写一个函数来解析

这些碎片,并引发错误。


< code>

public Const xyzcErrNumBase = 500


Public Const xyzcErrHeyDontDoThat = _

xyzcErrNumBase + 1& ":嘿!不要这样做!"


Public Sub RaiseCustomErr(strErrDefinition As String)

Dim lngColonPos As Long

Dim lngErrNum As Long

Dim strErrMessage As String

lngColonPos = InStr(strErrorSpec,":")

lngErrNum = CLng(Left $( strErrorSpec,lngColonPos - 1))

strErrMessage = Mid $(strErrorSpec,lngColonPos + 1)

Err.Raise lngErrNum ,, strErrMessage

End Sub


< ...>


RaiseCustomErr xyzcErrHeyDontDoThat


< .. 。>


< / code>

If you''ve ever employed custom error numbers and messages in you programs,
you''ve probably ended up with code similar to what I''ve ended up with in
the past something like...

<code>
public Const xyzcErrNumBase As Long = 500

Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1
Public Const xyzcErrTxtHeyDontDoThat = "Hey! Don''t do that!"

<...>

Err.Raise xyzcErrNumHeyDontDoThat, , xyzcErrTxtHeyDontDoThat

<...>

</code>
There is a duplication problem here. Every time you want to raise the
error, you have to pass both constants, and there''s nothing enforcing the
fact that the constants you pass are the ones that go together. It''s easy
to make a mistake (I''ve made them), and the mistake is likely to go
unnoticed until after it''s had you scratching your head for hours in a
debugging session at some point looking for the wrong error.

VB/VBA doesn''t offer much by way of help for this such as array constants,
etc., but I figured out a clever way to clean things up a bit. Combine the
number and decription into one string, then write a function to parse out
the pieces, and raise the error.

<code>
public Const xyzcErrNumBase = 500

Public Const xyzcErrHeyDontDoThat = _
xyzcErrNumBase + 1 & ":Hey! Don''t do that!"

Public Sub RaiseCustomErr(strErrDefinition As String)
Dim lngColonPos As Long
Dim lngErrNum As Long
Dim strErrMessage As String
lngColonPos = InStr(strErrorSpec, ":")
lngErrNum = CLng(Left$(strErrorSpec, lngColonPos - 1))
strErrMessage = Mid$(strErrorSpec, lngColonPos + 1)
Err.Raise lngErrNum, , strErrMessage
End Sub

<...>

RaiseCustomErr xyzcErrHeyDontDoThat

<...>

</code>

推荐答案

(strErrorSpec,lngColonPos - 1))

strErrMessage = Mid
(strErrorSpec, lngColonPos - 1))
strErrMessage = Mid


(strErrorSpec,lngColonPos + 1)

Err.Raise lngErrNum ,, strErrMessage
End Sub


< ...>


RaiseCustomErr xyzcErrHeyDontDoThat


< ...>


< /代码>

(strErrorSpec, lngColonPos + 1)
Err.Raise lngErrNum, , strErrMessage
End Sub

<...>

RaiseCustomErr xyzcErrHeyDontDoThat

<...>

</code>


no **** @ nospam.nosp am(Steve Jorgensen)写道:

< q1 ****************************** **@4ax.com>:
no****@nospam.nospam (Steve Jorgensen) wrote in
<q1********************************@4ax.com>:
如果您曾在自己的程序中使用自定义错误编号和消息,那么您可能最终得到的代码类似我过去最终得到的东西......

< code>
公共Const xyzcErrNumBase As Long = 500
Public Const xyzcErrTxtHeyDontDoThat ="嘿!不要这样做!"

< ...>

Err.Raise xyzcErrNumHeyDontDoThat ,, xyzcErrTxtHeyDontDoThat

<。 ..>

< / code>

此处存在重复问题。每当你想要提出错误时,你必须传递两个常数,并且没有什么可以强制执行你传递的常数是那些一起的常数。犯错很容易(我已经犯了错误),并且这个错误很可能会被忽视,直到它让你在调试中摸不着头几个小时会话在某些地方寻找错误的错误。

VB / VBA并没有提供太多帮助,例如数组
常量等,但是我想出了一个聪明的方法来清理一下
。将数字和描述合并为一个字符串,然后编写一个函数来解析碎片,并引发错误。

< code>
public Const xyzcErrNumBase = 500 <公共宪法xyzcErrHeyDontDoThat = _
xyzcErrNumBase + 1& ":嘿!不要这样做!"

Public Sub RaiseCustomErr(strErrDefinition As String)
Dim lngColonPos As Long
Dim lngErrNum As Long
Dim strErrMessage As String
lngColonPos = InStr(strErrorSpec,":")
lngErrNum = CLng(左
If you''ve ever employed custom error numbers and messages in you
programs, you''ve probably ended up with code similar to what I''ve
ended up with in the past something like...

<code>
public Const xyzcErrNumBase As Long = 500

Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1
Public Const xyzcErrTxtHeyDontDoThat = "Hey! Don''t do that!"

<...>

Err.Raise xyzcErrNumHeyDontDoThat, , xyzcErrTxtHeyDontDoThat

<...>

</code>
There is a duplication problem here. Every time you want to raise
the error, you have to pass both constants, and there''s nothing
enforcing the fact that the constants you pass are the ones that
go together. It''s easy to make a mistake (I''ve made them), and
the mistake is likely to go unnoticed until after it''s had you
scratching your head for hours in a debugging session at some
point looking for the wrong error.

VB/VBA doesn''t offer much by way of help for this such as array
constants, etc., but I figured out a clever way to clean things up
a bit. Combine the number and decription into one string, then
write a function to parse out the pieces, and raise the error.

<code>
public Const xyzcErrNumBase = 500

Public Const xyzcErrHeyDontDoThat = _
xyzcErrNumBase + 1 & ":Hey! Don''t do that!"

Public Sub RaiseCustomErr(strErrDefinition As String)
Dim lngColonPos As Long
Dim lngErrNum As Long
Dim strErrMessage As String
lngColonPos = InStr(strErrorSpec, ":")
lngErrNum = CLng(Left


这篇关于自定义错误号/消息提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆