Data Files - C Interview Questions VIII


Q. How can I avoid the Abort, Retry, Fail messages?
When DOS encounters a critical error, it issues a call to interrupt 24, the critical error handler. Your C compiler library contains a function named harderr() that takes over the handling of calls to interrupt 24. The harderr() function takes one argument, a pointer to a function that is called if there is a hardware error.
Your user-defined hardware error-handling function is passed information regarding the specifics of the hardware error that occurred. In your function, you can display a user-defined message to avoid the ugly Abort, Retry, Fail message. This way, your program can elegantly handle such simple user errors as your not inserting the disk when prompted to do so.
When a hardware error is encountered and your function is called, you can either call the C library functionhardretn() to return control to your application or call the C library function hardresume() to return control to DOS. Typically, disk errors can be trapped and your program can continue by using the hardresume() function. Other device errors, such as a bat FAT (file allocation table) error, are somewhat fatal, and your application should handle them by using the hardretn() function. Consider the following example, which uses the harderr() function to trap for critical errors and notifies the user when such an error occurs:
#include <stdio.h>
#include <dos.h>
#include <fcntl.h>
#include <ctype.h>
void main(void);
void far error_handler(unsigned, unsigned, unsigned far*);
void main(void)
{
     int file_handle, ret_code;
     /* Install the custom error-handling routine. */
     _harderr(error_handler);
     printf("\nEnsure that the A: drive is empty, \n");
     printf("then press any key.\n\n");
     getch();
     printf("Trying to write to the A: drive...\n\n");
     /* Attempt to access an empty A: drive... */
     ret_code = _dos_open("A:FILE.TMP", O_RDONLY, &file_handle);
     /* If the A: drive was empty, the error_handler() function was
        called. Notify the user of the result of that function. */
     switch (ret_code)
     {
          case 100: printf("Unknown device error!\n");
                    break;
          case 2:   printf("FILE.TMP was not found on drive A!\n");
                    break;
          case 0:   printf("FILE.TMP was found on drive A!\n");
                    break;
          default:  printf("Unknown error occurred!\n");
                    break;
     }
}
void far error_handler(unsigned device_error, unsigned error_val, 
                        unsigned far* device_header)
{
     long x;
     /* This condition will be true only if a nondisk error occurred. */
     if (device_error & 0x8000)
          _hardretn(100);
     /* Pause one second. */
     for (x=0; x<2000000; x++);
         /* Retry to access the drive. */
         _hardresume(_HARDERR_RETRY);
}
In this example, a custom hardware error handler is installed named error_handler(). When the program attempts to access the A: drive and no disk is found there, the error_handler() function is called. Theerror_handler() function first checks to ensure that the problem is a disk error. If the problem is not a disk error, it returns 100 by using the hardretn() function. Next, the program pauses for one second and issues ahardresume() call to retry accessing the A: drive.

Comments