Q(uick)BASIC Function: ERDEV
Quick View
ERDEV and ERDEV$
Error-trapping functions that provide device-specific status information after an error
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- ERDEV
- ERDEV$
Description/Parameter(s)
- ERDEV returns an error code from the last device that generated a critical error.
- The low byte of the value returned by ERDEV contains the DOS error code. The high byte contains device-attribute information.
- ERDEV$ returns the name of the device that generated the error.
Example
This example illustrates ERDEV, ERDEV$, ERL, ERR, ERROR, ON ERROR, and RESUME.
ON ERROR GOTO Handler
10 CHDIR "a:\" 'Causes ERR 71 "Disk not ready"
'if no disk in Drive A.
20 y% = 0
30 x% = 5 / y% 'ERR 11 "Division by zero."
40 PRINT "x% ="; x%
50 ERROR 57 'ERR 57 "Device I/O error."
Handler:
PRINT
PRINT "Error "; ERR; " on line "; ERL
SELECT CASE ERR
CASE 71
PRINT "Using device "; ERDEV$; " device error code = "; ERDEV
RESUME NEXT
CASE 11
INPUT "What value do you want to divide by"; y%
RESUME 'Retry line 30 with new value of y%.
CASE ELSE
PRINT "Unexpected error, ending program."
END
END SELECT
See also:
Syntax
- ERDEV
- ERDEV$
Description/Parameter(s)
ERDEV is an integer function that returns an error code from the last device to declare an error.
ERDEV$ is a string function that returns the name of the device generating the error. ERDEV$ will contain the 8-byte character device name if the error was on a character device, such as a printer. It will contain the 2-byte block name (A:, B:, etc.) if the device was not a character device.
Because ERDEV and ERDEV$ return meaningful information only after an error, they are usually used in error handlers specified by an ON ERROR statement.
ERDEV and ERDEV$ cannot be used on the left side of an assignment.
ERDEV is set by the critical error handler (interrupt 24H) when DOS detects an error that prevents continuing.
The value of ERDEV is a bit-encoded value containing the DOS error information. The lower eight bits (first byte) contain the DOS error code, a value from 0 to 12. The upper eight bits (second byte) contain bits 15, 14, 13, XX, 3, 2, 1, and 0, in that order, of the device-attribute word. XX indicates the bit is always zero. See the Microsoft DOS Programmer's Reference for more information about device-attribute words.
Examples
This example prints the values of ERDEV and ERDEV$ after the program generates an error attempting to open the file A:JUNK.DATA. Press the F4 function key to view the program output.
DEFINT A-Z
' Indicate first line of error handler.
ON ERROR GOTO ErrorHandler
' Attempt to open the file.
OPEN "A:JUNK.DAT" FOR INPUT AS #1
END
' Error handling routine.
' Prints values of ERDEV and ERDEV$ and dies.
ErrorHandler:
PRINT "ERDEV value is "; ERDEV
PRINT "Device name is "; ERDEV$
ON ERROR GOTO 0
Running the program with drive A unlatched produces the following output (2 is the DOS error code for "Drive not ready"):
Sample Output:
ERDEV value is 2 Device name is A:Syntax
- ERDEV
- ERDEV$
Description/Parameter(s)
- ERDEV$ contains the 8-byte character device name if the error is on a character device, such as a printer, or the 2-byte block device name (A:, B:, etc.) if the device is not a character device. It contains the 3-byte block name (COM) if a communications port experiences a timeout.
- ERDEV is set by the critical error handler (interrupt 24H) when DOS detects a critical DOS call error from a character or block device. It also is set by a timeout error on a communications port.
- ERDEV returns an integer value that contains information about the error. The format of the information depends upon the source of the error.
- For block device errors, bits 0-7 (the low byte) of ERDEV contain the DOS error code. Bits 8-15 (the high byte) contain device-attribute information:
Bit(s) | Value | Meaning |
0-7 | xxH | Contains DOS error code xxH |
8 | 0 | Read operation |
1 | write operation | |
9-10 | Indicate the affected disk area: | |
0 | MS -DOS | |
01 | File allocation table | |
10 | Root directory | |
11 | files area | |
11* | 0 | Fail response not allowed |
1 | Fail response allowed | |
12* | 0 | Retry response not allowed |
1 | Retry response allowed | |
13* | 0 | Ignore response not allowed |
1 | Ignore response allowed | |
14 | Not used | |
15 | 0 | Always zero |
*bits 11-13 apply only to MS-DOS versions 3.1 and later. |
- For character device errors, bits 0-7 contain the DOS error code. The upper bits are not significant.
- For COM timeout errors, ERDEV returns a value indicating which option in the OPEN COM statement is experiencing the timeout. Therefore, it's not necessary to break ERDEV into low and high bytes.
ERDEV Value | OPEN COM Option Timeout |
80H | CS (CTS timeout) |
81H | DS (DSR timeout) |
82H | CD (RLSD timeout) |
Usage Notes
- Because ERDEV and ERDEV$ return meaningful information only after an error, they usually are used in error handlers specified by an ON ERROR statement.
- ERDEV and ERDEV$ cannot be used on the left side of an assignment.
- Use ERDEV and ERDEV$ only in DOS.
- The following example program lines extract the low and high bytes from the ERDEV value. DosErrCode stores the block or character device error code. DevAttr stores the block device attribute information.
DosErrCode = ERDEV AND &HFF 'low byte of ERDEV
DevAttr = (ERDEV AND &HFF00) \ 256 'high byte of ERDEV
- For more information about device attributes, see the Microsoft MS-DOS Programmer's Reference, or books such as The Peter Norton Guide to the IBM PC or Advanced MS-DOS.
Examples
This example prints the values of ERDEV and ERDEV$ after the program generates an error attempting to open a text file.
DEFINT A-Z
'Indicate first line of error handler.
ON ERROR GOTO ErrorHandler
'Attempt to open the file.
OPEN "A:JUNK.DAT" FOR INPUT AS #1
END
'Error handling routine.
'Prints values of ERDEV and ERDEV$ and returns error control to BASIC.
ErrorHandler:
PRINT "ERDEV value is "; ERDEV
PRINT "Device name is "; ERDEV$
ON ERROR GOTO 0
Running the program with drive A unlatched produces the following output (2 is the DOS error code for "Drive not ready"):
Sample Output:
ERDEV value is 2 Device name is A: