Tuesday, December 15, 2009

Chapter 18. Programming with Exceptions




I l@ve RuBoard

Chapter
18. Programming with Exceptions






Topics in this Chapter








  • A Simple Example of Exception Processing






  • Syntax of C++ Exceptions






  • Exceptions with Class Objects






  • Type Cast Operators






  • Summary






In this chapter, I will deal with a relatively new C++ topic: programming with exceptions. Exception is a language mechanism that allows the programmer to separate source code that describes the main case of processing from source code that describes exceptional situations. Exceptional situations are situations that should not occur during normal processing but from time to time do occur. Separating this exception processing from the main case makes the main case easier to read and to maintain. It also makes exceptional cases easier to read and to maintain.



This definition is somewhat vague, is it not? It does leave room for interpretation. Indeed, what some people view as an exceptional or abnormal situation, other people perceive as a genuine part of system operations. For example, when you allocate memory on the heap, the algorithm should describe what happens if the request is satisfied. Since it is possible that the computer runs out of memory, the algorithm should also specify what happens when memory is not available. Is running out of memory an exception? Most people would say yes.



Similarly, when the program reads data interactively from the online user, the algorithm specifies the processing of valid data. What happens if the user makes a mistake and inputs data that is invalid? Is this an exception? Most people would say no, online mistakes are a way of life, and the algorithms for processing these mistakes should be viewed as part of basic system functionality, not something that happens only rarely.



Similarly, when you read data from a file in a loop, the algorithm specifies what happens when the next record is read梙ow different parts of the record should be processed. Since it is possible that there are no more records in the file, the algorithm should define what happens when there are no more records to read. Is reaching the end-of-file indeed an exception? Most people would say no, this is an event that marks the end of one stage of processing (reading file records in) and the beginning of the next stage of processing (computations on data in memory).



Regardless of whether the programmer perceives the situation as mainline processing with some additional exceptional cases (the first example) or as a set of different cases of approximately equal importance (the second and the third examples), the issue of clogging the source code with diverse computational tasks is both real and important.



To be able to make intelligent decisions on structuring your algorithms, you should have a good understanding of the tools available in the programming language. This is why I will try first and foremost to explain what exceptions (as a C++ programming technique) are, what syntax they impose on the programmer, how to use them correctly, and what incorrect uses you should try to avoid.



Initially, C++ did not support exceptions and relied on C mechanisms for exception processing by using global variables accessible to the whole program (e.g., errno) or jumps and calls to special functions whose names are fixed but whose contents might be specified by the programmer (e.g., setjmp and longjmp).



The C++ exception facility is a relatively new language feature. Similar to C++ templates, the exception mechanism is complex. The experience in using exceptions is rather limited, and the advantages of their use for system design are not demonstrated yet. In addition, the use of C++ exceptions increases the program execution time and the size of the executable program. This is why I do not think you should use exceptions at every opportunity that presents itself. Eventually, however, they should become a legitimate part of your programming toolbox.







I l@ve RuBoard

No comments: