|
![]() |
|||
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Technique Another way to handle error conditions in your code is to perform what are known as non-local jumps. This means if an error occurs in a function, you can jump to a particular line of code. Typically, that line of code is just prior to where the function was originally called. Using the setjmp and longjmp functions provided with the Standard C Library, you can perform non-local jumps to handle error conditions and still maintain the state of the callers stack environment. This means when control is set back to the calling function, its local variables still have the same values. This How-To will discuss how to use setjmp and longjmp together to perform error handling. This How-To will also discuss how they work and why it is not safe to use these functions in C++ programs. Steps
How It Works The setjmp function records the current state of the stack environment. The jmp_buf typedef is an implementation-defined array. After recording the current stack environment, setjmp returns 0. A call to longjmp will restore the stack environment that was saved when setjmp was called. When calling longjmp, you specify the variable used to save the stack environment and a return code. Calling longjmp causes the program to return from setjmp, again returning the return code that was given to longjmp as the second parameter. When setjmp returns, all variables, except register variables, that are accessible by the function calling setjmp have the same values they had when the longjmp function was called. The sample code in the preceding Steps section first calls setjmp to save the current state. The return value from this first call to setjmp is 0, so you call the DoSomeWork function. This function attempts to open a file that does not exist. If the open fails, DoSomeWork calls longjmp and passes to it the stack environment previously saved and a return code of -1. This causes setjmp to be returned from again. This time the return value from setjmp is -1. Therefore, the program exits. Comments Using setjmp and longjmp, you can implement another form of error handling in your programs. However, you should not use setjmp and longjmp in C++ programs. These functions do not support the semantics of C++ objects. That means destructors for objects will not be called when longjmp is called. In C++ programs, you should use the C++ exception-handling constructs that are talked about in Chapter 11, Exception Handling in C++. 10.9 Use a C++ class to handle runtime errors in a more maintainable fashion?Problem I understand the importance of error handling in my programs. I also understand how to return values from functions. Is there a better way to keep track of error information in my programs? Technique Returning error values from functions is a very efficient way to handle errors. However, the meanings of the return values of many different functions can be very difficult to keep track of. They are also very difficult to maintain, especially if you are not the original developer of the code and the comments are poorly written or nonexistent. This section provides a sample class called ErrorClass that can be used as the return value for a function. This class takes care of maintaining error codes and provides human-readable descriptions. You can then modify this class to suit your own needs. Steps This section discusses how to use this sample class in your programs. The actual class itself will be discussed in the How It Works section.
|
![]() |
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.
|