Chapter 16: Exception Handling
873
This is not to say that asynchronous events cannot be associated with exceptions. But the interrupt handler should do its job as quickly as possible and then return. Later, at some well-defined point in the program, an exception might be thrown based on the interrupt.
Not for ordinary error conditions
If you have enough information to handle an error, it’s not an exception. You should take care of it in the current context rather than throwing an exception to a larger context.
Also, C++ exceptions are not thrown for machine-level events like divide-by-zero. It’s
assumed these are dealt with by some other mechanism, like the operating system or
hardware. That way, C++ exceptions can be reasonably efficient, and their use is isolated to
program-level exceptional conditions.
Not for flow-of-control
An exception looks somewhat like an alternate return mechanism and somewhat like a switch
statement, so you can be tempted to use them for other than their original intent. This is a bad idea, partly because the exception-handling system is significantly less efficient than normal program execution; exceptions are a rare event, so the normal program shouldn’t pay for
them. Also, exceptions from anything other than error conditions are quite confusing to the
user of your class or function.
You’re not forced to use exceptions
Some programs are quite simple, many utilities, for example. You may only need to take
input and perform some processing. In these programs you might attempt to allocate memory
and fail, or try to open a file and fail, and so on. It is acceptable in these programs to use assert( ) or to print a message and abort( ) the program, allowing the system to clean up the mess, rather than to work very hard to catch all exceptions and recover all the resources
yourself. Basically, if you don’t need to use exceptions, you don’t have to.
New exceptions, old code
Another situation that arises is the modification of an existing program that doesn’t use
exceptions. You may introduce a library that does use exceptions and wonder if you need to modify all your code throughout the program. Assuming you have an acceptable error-handling scheme already in place, the most sensible thing to do here is surround the largest
block that uses the new library (this may be all the code in main( )) with a try block, followed by a catch(...) and basic error message. You can refine this to whatever degree necessary by adding more specific handlers, but, in any case, the code you’re forced to add can be minimal.
You can also isolate your exception-generating code in a try block and write handlers to
convert the exceptions into your existing error-handling scheme.
It’s truly important to think about exceptions when you’re creating a library for someone else to use, and you can’t know how they need to respond to critical error conditions.
Chapter 16: Exception Handling
874
Typical uses of exceptions
Do use exceptions to

9.
Fix the problem and call the function (which caused the exception) again.

10.
Patch things up and continue without retrying the function.

11.
Calculate some alternative result instead of what the function was supposed
to produce.

12.
Do whatever you can in the current context and rethrow the same exception
to a higher context.

13.
Do whatever you can in the current context and throw a different exception
to a higher context.

14.
Terminate the program.

15.
Wrap functions (especially C library functions) that use ordinary error
schemes so they produce exceptions instead.

16.
Simplify. If your exception scheme makes things more complicated, then it
is painful and annoying to use.

17.
Make your library and program safer. This is a short-term investment (for
debugging) and a long-term investment (for application robustness).
Always use exception specifications



 

 

 

 

 

 

 

 

 

 

 

 

   
 
  whereas asynchronous events must be handled by completely separate code that is not part of the normal program flow (typically, interrupt service routines or event...
Pomodliłem się do każdego boga jaki istniał bym był w wstanie wkurzyć tę kobietę do granic możliwości.