![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Comments As you can see, the way the Standard C Library handles errors is very efficient and maintainable. Also, it ensures that the error values are consistent across all functions in the library. You could do something similar in your applications by creating your own version of the errno.h file and sys_errlist array. The only downside to this approach is that you have to recompile your application every time you add an error code. An alternative to keeping these error codes in a header file is to keep them in a database. When your application starts, it can load an array with all the error codes and descriptions in the database. Your startup time will be longer, but your execution time will not change. I feel that the advantages of this approach far outweigh the minimal increase in startup time. 10.3 Use assert to catch errors in my code when running in debug mode?Problem I want to be able to catch errors in my code before I ship my application. Is anything provided with the Standard C Library that will help me catch errors before my application is released? Technique Before releasing your application for general use, it is imperative that it goes through rigorous testing. In order to catch errors when you are testing, it is important that your code tests different conditions and reports anomalies to the tester. Also, if an anomalous condition occurs, you will want to terminate your program so you can investigate why the problem occurred. Executing your application in this context is referred to as running your application in debug mode. However, when your product is released, you dont want your application to terminate if an anomalous condition occurs. In that situation, your application might be able to write a log message to a file and continue executing. The Standard C Library provides the assert function that can be used to test the validity of an expression. This expression can be anything that can be evaluated to true or false, such as whether a pointer is null. If the expression is false, assert prints a diagnostic message and aborts the program. You can then use this diagnostic message to investigate the cause of the error condition. Steps
How It Works The assert function takes as input an integer variable that evaluates to true (not equal to 0) or false (0). If the value is false, assert prints a diagnostic message and aborts the program by calling the abort function in ANSI C implementations or exit in traditional C implementations. These functions are discussed later in this chapter. The diagnostic message includes the name of the file and the line number at which the error occurred. When you are ready to release your program for general use, you can turn off assertions by defining the macro NDEBUG either in your code or on the command line when compiling. To include this in your code, just add the following line to an include file that is included in all your source code files. #define #NDEBUG 1 Including this macro on the command line when compiling will be different for each compiler. Therefore, check the documentation for your compiler for instructions about how to do this. Comments Using assert in your code can greatly aid in documentation for those who are maintaining your program and is also very helpful when debugging. When you have finished testing your program, you can then simply turn off assertions by defining the NDEBUG macro. Assertions in your code have no runtime overhead when they have been disabled by defining the NDEBUG macro. 10.4 Use raise and signal to indicate errors in my programs?Problem Is there any way to handle runtime errors in my program other than returning error codes from functions? Technique In certain situations, returning an error code is just not possible, such as when a user interrupts the program by pressing Ctrl+C. In these situations, an error is triggered or raised by the computers error detection mechanism. This error is raised by the system and often will cause your application to terminate. When this error occurs, you might wish to try to recover or you might want to perform some clean up and exit gracefully. Fortunately, the Standard C Library provides the signal function that can be used to catch the errors raised by the system. When the error is raised, the function you specify in a call to signal will be executed. But what if you want to do something similar to this in your code? Is it possible for you to raise an error in your code? The Standard C Library provides the raise function for just this reason. Using raise and signal together, you can implement an alternative error handling technique. The sections that follow will show you how to do this and how it works. Steps
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|