Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



Raising an Error


You use the Raise method of the error object in these situations:

  • When you want to generate an error on purpose (for example, in testing)

  • When you want to generate a user-defined error

  • When no code in the error routine handles the current error, and you want to allow other parts of the call stack to attempt to handle the error

  • When you want to nest an error handler


Using the Raise method to generate an error on purpose and creating a user-defined error are both complicated and important enough that they require special attention. They are covered in the following sections.

Generating an Error on Purpose


Many times during testing, you want to generate an error so that you can check your own error handling. Instead of figuring out how to cause the error condition, you can use the Raise method of the Err object to accomplish this task, as Listing 16.10 shows.

Listing 16.10 Raising an Error

Sub TestRaiseError()
'Invoke error handling
On Error GoTo TestRaiseError_Err
Dim sngResult As String
'Raise a divide-by-zero error
Err.Raise 11
'Exit the subroutine
Exit Sub
TestRaiseError_Err:
'Display a message with the error number and description
MsgBox "Error #" & Err.Number & ": " & Err.Description
'Exit the subroutine
Exit Sub
End Sub

This code invokes an error 11 (divide by 0).

Creating User-Defined Errors


Another important use of the Raise method of the Err object is the generation of a custom error condition. This is useful when you want to

force an error in response to something that the user did. For example, assume that the user must enter five characters into an unbound text box. Entering only two characters would not generate an Access error. Rather than handling this

user-generated error in some other manner, you can raise the error and have your standard error handler respond to the error condition. Because the Raise method enables you to set all the properties of the Err object, you can create a user-defined error complete with a number, description, source, and so on, as shown in Listing 16.11.

Listing 16.11 Creating a User-Defined Error

Sub TestCustomError()
'Invoke error handling
On Error GoTo TestCustomError_Err
Dim strName As String
'Prompt the user to enter their name
strName = InputBox("Please Enter Your Name")
'If the length of the name is less than five
'characters, raise an error number 11111
If Len(strName) < 5 Then
Err.Raise Number:=11111, _
Description:="Length of Name is Too Short"
Else
MsgBox "You Entered " & strName
End If
Exit Sub
TestCustomError_Err:
'Display a message with the error number
'and description
MsgBox "Error # " & Err.Number & _
" - " & Err.Description
Exit Sub
End Sub

Although it is very simple, Creating a Generic Error Handler," later in this chapter, explores how to put together a generic error handler. By passing user-defined errors through your generic error handler, all errorsuser-defined or notare handled in the same way.


/ 544