|
INPRISE Online And ZD Journals Present:
If your application uses floppy disks, then you should be prepared to handle any potential disk errors. A disk error occurs when theres no disk in a floppy drive, when a disk in the floppy drive isnt formatted, when the disk is damaged, or for many other reasons. Handling disk errors gracefully makes your application more robust and saves your users from endless frustration. In this article, well explain how to handle disk errors in your application. Well also look at the SetErrorMode and GetLastError functions of the Windows API.
Why handle disk errors?
Figure A: An error
message box for an unformatted disk (Windows NT 4).
While handling critical errors at the operating-system level is fine for many applications, some applications need better control of such errors. For example, if your application writes to diskette, you may want to suppress the normal Windows error message box in order to do some processing behind the scenes. Or maybe you just dont like the message boxes that Windows provides and you want to replace them with your own message boxes. Whatever the reason, you occasionally need to take control of critical errors from Windows.
Two steps to success
Setting the error mode
SetErrorMode(SEM_FAILCRITICALERRORS);
After youve called this function, Windows will no longer handle critical errors. Thats not the whole story, though. Make sure you save the old error mode and reset it when youre finished with a particular operation. Weve done so in this example:
int OldMode; OldMode = SetErrorMode(SEM_FAILCRITICALERRORS); // Do some things that might cause an error // and then reset the error mode. SetErrorMode(OldMode);
As you might have surmised, SetErrorMode returns the previous error mode setting, thus allowing you to save the current error mode so you can restore it after youve completed a particular operation. Reset the error mode as soon as youve finished your processing. If you fail to do so, then the user may not receive notification of certain errors that are out of your control as a programmer. In short, do the following:
By following this guideline, your applications can have custom error handlingyet not interfere with normal Windows operations. By the way, you can catch other errors besides critical errors. See the SetErrorMode topic in the Win32 API help file for complete information.
Detecting errors
OldMode = SetErrorMode(SEM_FAILCRITICALERRORS); CopyFile("test.dat", "A:\\test.dat", false); int error = GetLastError(); // code to handle error here SetErrorMode(OldMode);
Since you could get any number of errors from this operation, you must be prepared to handle them all. For example, the CopyFile function in the previous example could result in any of the following errors:
And this is just a sampling. Other errors could also apply. (Note: For a complete list of error codes, check out WINERROR.H. This header file contains a complete list of Windows error codes and a brief description of each.) You must take great care to handle any possible errors that might occur as the result of a file operation (or any other operation for that matter). You dont need to handle each and every error specifically, but you must at least let the user know about errors that you arent handling. (See the sidebar "Getting Error Message Text" for a description of how to obtain the Windows error message text for errors that you dont handle directly.) You should also be aware of one other thing concerning GetLastError. Windows NT and Windows 95 dont necessarily return the same error code for identical disk error conditions. For example, if you attempt to set the current directory to a floppy drive that contains an unformatted disk under Windows NT, GetLastError will report an error code of ERROR_UNRECOGNIZED_MEDIA. On the other hand, under Windows 95 GetLastError might report ERROR_GEN_FAILURE or ERROR_INVALID_DRIVE for the same operation. Be sure to test your applications thoroughly under both operating systems so that you know your code will work on either platform.
Conclusion Custom error handling is vital in certain types of applications and a nice feature in others. Although you might not need SetErrorMode often, when you do, you should at least know how it works.
Kent Reisdorph is a senior software engineer at TurboPower Software and a member of TeamB, Borland's volunteer online support group. He's the author of Teach Yourself C++Builder in 21 Days and Teach Yourself C++Builder in 14 Days. You can contact Kent at kentr@turbopower.com. Back to Top Back to 1998 Index of Articles Copyright © 1998, Ziff-Davis Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of Ziff-Davis Inc. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis is prohibited. |