Sams Teach Yourself C++ in 21 Days

(singke) #1
generated for each line of your source code. You can examine the memory registers and
flags, and generally delve as deep into the inner workings of your program as required.
Learn to use your debugger. It can be the most powerful weapon in your holy war against
bugs. Runtime bugs are the hardest to find and squash, and a powerful debugger can
make it possible, if not easy, to find nearly all of them.

Summary ..............................................................................................................


Today, you learned the basics for creating and using exceptions. Exceptions are objects
that can be created and thrown at points in the program where the executing code cannot
handle the error or other exceptional condition that has arisen. Other parts of the pro-
gram, higher in the call stack, implement catchblocks that catch the exception and take
appropriate action.
Exceptions are normal, user-created objects, and as such can be passed by value or by
reference. They can contain data and methods, and the catchblock can use that data to
decide how to deal with the exception.
It is possible to create multiple catchblocks, but after an exception matches a catch
block’s signature, it is considered to be handled and is not given to the subsequent catch
blocks. It is important to order the catchblocks appropriately so that more specific
catchblocks have first chance, and more general catchblocks handle those not other-
wise handled.
Today’s lesson also mentioned the fundamentals of symbolic debuggers, including using
watch points, breakpoints, and so forth. These tools can help you zero in on the part of
your program that is causing the error and let you see the value of variables as they
change during the course of the execution of the program.

Q&A ....................................................................................................................


Q Why bother with raising exceptions? Why not handle the error right where it
happens?
A Often, the same error can be generated in different parts of the code. Exceptions let
you centralize the handling of errors. In addition, the part of the code that gener-
ates the error might not be the best place to determine how to handle the error.
Q Why generate an object? Why not just pass an error code?
A Objects are more flexible and powerful than error codes. They can convey more
information, and the constructor/destructor mechanisms can be used for the

748 Day 20

Free download pdf