![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
If you dont set a handler function for a signal or specify one of the special values, the signal has a default action. Table 10.4 lists the default action for each of the standard signals listed in Table 10.3.
Comments By using raise and signal, you can implement an alternative error handling technique into your programs. Instead of returning errors from functions, you can set handler functions that are called when a signal is raised. This also allows you to perform such actions as freeing memory when your program is abnormally terminated. When compiling the preceding sample code, you might experience warnings if you are using a compiler that is not ANSI Ccompliant or does not support the latest features of the C++ language. 10.5 Use abort to terminate my application if a serious error occurs?Problem I have run into a serious error condition from which I cannot recover in my program. How can I terminate my application? Is there any way to execute a body of code when my program is terminated? Technique In the unfortunate situation in which a serious error occurs and you cant recover from it, it might be necessary to terminate your application. The Standard C Library provides the abort function that enables you to terminate your application abnormally. When this abnormal termination occurs, you might wish to perform some action, such as freeing memory. Using the signal function described earlier, you can specify a function that is called when the program is abnormally terminated. The following sections will discuss how to use abort to terminate your application and how to use signal to execute a function when abort is called. Steps
How It Works When called, abort prints the message abnormal program termination, and then raises the SIGABRT signal. If you have set a signal handler for SIGABRT, your handler function will be called. The code listed in the Steps section sets a handler function for the SIGABRT signal. Then it allocates some memory and calls abort. The handler function, AbortHandler, is then called. This function prints a diagnostic message and then frees the memory that was allocated in main. When AbortHandler returns, the program is terminated. Comments Using abort in your programs is an easy way to terminate your application if a condition occurs from which you cannot recover. Setting a handler function for SIGABRT allows you to perform some action when your program is aborted, such as cleaning up previously allocated memory. This means any time your program is aborted, whether or not you are the one calling abort, you can be assured that all allocated resources have been freed. 10.6 Use exit and atexit together to perform some action when my program terminates normally?Problem I would like to exit my application and return a code from my application. Also, I would like a function to be executed automatically when my application terminates. How can I do this? Technique In order to exit your application, you can use the abort function as mentioned earlier. However, abort is usually used for abnormal program termination. Therefore, the Standard C Library provides the exit function that can be used to exit your program. This function can also be used to return error or success codes from your application. In addition, the atexit function is provided to enable you to specify a function that will be called any time the exit function is called. The following sections describe how to use exit and atexit and how they work. 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.
|