HP aC++/HP C Programmer's Guide (B3901-90036; A.06.26; September 2011)
8 Exception Handling
Exception handling provides a standard mechanism for coding responses to runtime
errors or exceptions.
This chapter discusses the following topics:
• “Exception Handling in C++” (page 200)
• “Exception Handling as Defined by the ANSI/ISO C++ International Standard”
(page 201)
• “Basic Exception Handling Example” (page 201)
• “Function Try Block Examples” (page 201)
• “Debugging Exception Handling” (page 202)
• “Performance Considerations” (page 202)
Exception Handling
Exception handling provides a standard mechanism for coding responses to runtime
errors or exceptions. Exception handling is on by default. To turn it off, you must use the
+noeh option.
If your executable throws no exceptions, object files compiled with and without the +noeh
option can be mixed freely. However, in an executable which throws exceptions (HP
aC++ runtime libraries throw exceptions), you must be certain that no exception is thrown
in your application which will unwind through a function compiled without the exception
handling option turned on.
In order to prevent this, the call graph for the program must never have calls from functions
compiled without exception handling to functions compiled with exception handling
(either direct calls or calls made through a callback mechanism). If such calls do exist,
and an exception is thrown, the unwinding can cause:
• Non-destruction of local objects (including compiler generated temporaries).
• Memory leaks when destructors are not executed.
• Runtime errors when no catch clause is found.
Exception Handling in C++
Following is an overview of the elements of C++ exception handling:
• A try block encloses (logically) code that can cause an exception that you want to
catch.
• A catch clause, which immediately follows the try block, handles an exception of
the type that can occur in the try block. The catch clause is the exception handler.
You can have multiple catch clauses associated with a try block.
200 Exception Handling