Select the product you need help with
EXCEPTEX Traps MFC and Win32 Structured ExceptionsArticle ID: 167802 - View products that this article applies to. This article was previously published under Q167802 On This PageSUMMARY EXCEPTEX is a sample that demonstrates techniques for
trapping of both Win32 Structured Exceptions and C++ Exceptions. For C++
exceptions, various helper functions are provided that demonstrate how to crack
open the more commonly used exception classes. For Win32 Structured Exceptions,
a helper function is provided that identifies the type of Win32 exception
thrown. The following files are available for download from the Microsoft Download Center: Exceptex.exe
(http://download.microsoft.com/download/vc50ent/sample/1/nt4/en-us/exceptex.exe)
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base: 119591 Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help to
prevent any unauthorized changes to the file.
(http://support.microsoft.com/kb/119591/EN-US/
)
How to Obtain Microsoft Support Files from Online Services
MORE INFORMATIONMerging Win32 and C++ Exception HandlingC++ Exception handling and Win32 Structured Exception Handling use different syntax to trap exceptions.You can, however, merge both C++ and Win32 Exception handling via the _set_se_translator() function. This causes a Win32 exception to be passed to the handler function of your choice, which can wrap Win32 Exception information in a class, and "throw" it as a C++ exception. This happens automatically when a Win32 Exception occurs, and appears seamlessly to the program as a C++ exception (see Exception Handling Differences in the VC 5.0 infoviewer). This is done for you automatically if you use the LOG macros and related support Log() functions. Types of C++ ExceptionsThere are several categories of C++ exceptions that can be caught by an MFC- based program:
Below is an example of the declaration for the overloaded LogException function that catches a pointer to the MFC CException class. Each of these helper functions cracks open the exception and stores the results in a string array that you can process as necessary. The function for dumping the contents of this string array to the TRACE() macro is LogDisplay(). Simplifying try/catch Blocks with Helper MacrosLittering try/catch blocks in your code can get messy after a while. However, it is possible to simplify exception blocks through the use of macros. EXCEPTEX provides three macros to simplify this process, and each offers different functionality for logging/handling of exceptions. Each of the macros are also written to work for both Visual C++ 4.X and Visual C++ 5.0, so they must check the version of the compiler you are using.Below is the simplest of the macros, LOGQ (quiet Exception Handling with no logging). It expects that a Boolean variable named bRetVal is available in your code. It must be true so that the macro can allow execution of the code that may raise an exception, and it sets it to FALSE if an exception is raised. You would use LOGQ as shown below: The final LOG macro provided by EXCEPTEX, LOGR, combines both LOGQ and LOGE. It checks to see if it should execute the code and log any exception that is raised. Other Useful FunctionsThe EXCEPTEX sample provides three more functions to assist exception handling:LogDisplay() Dumps contents of all logged exceptions out via TRACE macros.
LogDisplay( ... ) Dumps contents of all logged exceptions out to an instance of CListBox. LogSaveToFile( ... ) Dumps contents of all logged exceptions out to a file. LOG.H and LOG.CPPLOG.H and LOG.CPP contain all of the code in EXCEPTEX to handle exceptions. The remainder of the sample is to demonstrate the macros/functions found in LOG.H and LOG.CPP. These two files can be easily added to an existing project, and have been written to compile in either Visual C++ 4.X or 5.0 under both ANSI and UNICODE builds. To use them, add #include "log.h" to the files that will be using the LOGE or LOGR macro. If you want to crack Win32 Structured Exceptions also, you must call LogEnable once near the beginning of your program.You may not always want to use the LOG macros or even the LogException() helpers as provided. They are offered both as a convenience and as well as to demonstrate comprehensive exception handling, and for logging the results of any exception caught. They are not provided as the end-all, be- all for exception handling in every production environment. TroubleshootingThe following code shows how not to use the helper macros/W4 and #importIf you are using the /W4 flag in Visual C++ and #import, you will see 8 warnings generated off the include files that #import utilizes.
comutil.h(905) : warning C4310: cast truncates constant value These error messages can be ignored and should not affect your
code. comutil.h(928) : warning C4310: cast truncates constant value comutil.h(1030) : warning C4310: cast truncates constant value comutil.h(1281) : warning C4310: cast truncates constant value comutil.h(1307) : warning C4310: cast truncates constant value comutil.h(1476) : warning C4310: cast truncates constant value comdef.h(242) : warning C4244: 'return' : conversion from 'int' to 'unsigned short', possible loss of data For More InformationThe following VC++ 5.0 infoviewer topic present a good introduction to exception handling techniques:
Exception Handling: Frequently Asked Questions The following article demonstrates techniques that could be used
to expand the EXCEPTEX sample to handle DAO SDK exceptions. mk:@ivt:vccore/F26/D2A/S31BE4.HTM Exception Handling Differences mk:@ivt:vccore/F26/D2B/S4CC99.HTM 152695 A definitive source on Win32 Exception Handling can be found in:
(http://support.microsoft.com/kb/152695/EN-US/
)
How to Catch and Decipher DAO SDK-Based Exceptions
"Advanced Windows" by Jeffrey Richter, ISBN 1-57231-548-2 PropertiesArticle ID: 167802 - Last Review: August 5, 2004 - Revision: 3.2 APPLIES TO
|



Back to the top








