19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

222 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Type of error Value of Err.Number<br />

Subscript out of range 9<br />

Division by zero 11<br />

File not found 53<br />

File already open 55<br />

File already exists 58<br />

Disk full 61<br />

Disk not ready 71<br />

To set up error-trapping inside a procedure, do the following:<br />

1. Make the first line of the procedure<br />

On Error GoTo ErrorH<strong>and</strong>ler<br />

2. Type in the lines to carry out the purpose of the procedure.<br />

3. Make the last lines of the procedure<br />

Exit SubErrorH<strong>and</strong>ler:error-h<strong>and</strong>ling routineResume<br />

The statement “On Error GoTo ErrorH<strong>and</strong>ler” activates error-trapping. If an error occurs<br />

during the execution of a line of the procedure, the program will jump to the error-h<strong>and</strong>ling<br />

routine. The statement “Resume” causes the program to jump back to the line causing the<br />

error. The statement “Exit Sub”, which causes an early exit from the procedure, prevents the<br />

error-h<strong>and</strong>ling routine from being entered when no error occurs. For instance, the following<br />

procedure has an error-h<strong>and</strong>ling routine that is called when a file cannot be found.<br />

Private Sub OpenFile()<br />

On Error GoTo ErrorH<strong>and</strong>ler<br />

Dim fileName As String<br />

fileName = InputBox(“Enter the name of the file to be opened.”)<br />

Open fileName For Input As #1<br />

Exit Sub<br />

ErrorH<strong>and</strong>ler:<br />

Select Case Err.Number<br />

Case 53<br />

MsgBox “File not found. Try Again.”<br />

fileName = InputBox(“Enter the name of the file to be opened.”)<br />

Case 71<br />

MsgBox “The drive door might be open - please check.”<br />

End Select<br />

Resume<br />

End Sub<br />

The word “ErrorH<strong>and</strong>ler”, which is called a line label, can be replaced by any word of<br />

at most 40 letters. The line, which is placed just before the error-h<strong>and</strong>ling routine, must start<br />

at the left margin <strong>and</strong> must end with a colon. If “Resume” is replaced by “Resume Next”,<br />

then the program will jump to the line following the line causing the error.<br />

The line label must be in the same procedure as the On Error statement. However, the<br />

error-h<strong>and</strong>ling routine can call another procedure.<br />

There are two variations of the On Error statement. The statement “On Error GoTo 0”<br />

turns off error-trapping. The statement “On Error Resume Next” specifies that when a runtime<br />

error occurs, execution continues with the statement following the statement where the<br />

error occurred.<br />

COMMENTS<br />

1. Sequential files make efficient use of disk space <strong>and</strong> are easy to create <strong>and</strong> use.<br />

Their disadvantages are as follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!