home *** CD-ROM | disk | FTP | other *** search
- // ExTrans.cpp: Example program demonstrating an exception translator function
- // for translating a Win32 exception to a C++ exception
-
- #include <windows.h>
- #include <iostream.h>
- #include <eh.h>
-
- class CSExcept
- {
- public:
- CSExcept (unsigned int ExCode)
- {
- m_ExCode = ExCode;
- }
- unsigned int GetExCode ()
- {
- return m_ExCode;
- }
-
- private:
- unsigned int m_ExCode;
- };
-
- void SETranslate (unsigned int ExCode, _EXCEPTION_POINTERS *PtrExPtrs)
- {
- throw CSExcept (ExCode);
- }
-
- void main ()
- {
- char Ch;
-
- _set_se_translator (SETranslate);
-
- try
- {
- // ...
- cout << "generate 'integer divide by zero' exception? (y/n): "; cin >> Ch;
- if (Ch == 'y' || Ch == 'Y')
- {
- int I = 0;
- int J = 5 / I;
- }
- cout << "generate 'access violation' exception? (y/n): "; cin >> Ch;
- if (Ch == 'y' || Ch == 'Y')
- {
- *((char *)0) = 'x';
- }
- // other statements which may cause other exceptions...
- }
- catch (CSExcept SExcept)
- {
- switch (SExcept.GetExCode ())
- {
- case EXCEPTION_INT_DIVIDE_BY_ZERO:
- cout << "'integer divide by zero' exception occurred" << '\n';
- break;
-
- case EXCEPTION_ACCESS_VIOLATION:
- cout << "'access violation' exception occurred" << '\n';
- break;
-
- default:
- cout << "unknown exception occurred" << '\n';
- throw;
- break;
- }
- }
- }
-