9

Exceptions

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void MyFunc( void );
  6.  
  7. class CTest
  8. {
  9. public:
  10.     CTest();
  11.     ~CTest();
  12.     const char *ShowReason() const { return "Exception in CTest class."; }
  13. };
  14.  
  15. CTest::CTest()
  16. {
  17.         cout << "Constructing CTest." << endl;
  18. }
  19.  
  20. CTest::~CTest()
  21. {
  22.         cout << "Destructing CTest." << endl;
  23. }
  24.  
  25. class CDtorDemo
  26. {
  27. public:
  28.     CDtorDemo();
  29.     ~CDtorDemo();
  30. };
  31.  
  32. CDtorDemo::CDtorDemo()
  33. {
  34.     cout << "Constructing CDtorDemo." << endl;
  35. }
  36.  
  37. CDtorDemo::~CDtorDemo()
  38. {
  39.     cout << "Destructing CDtorDemo." << endl;
  40. }
  41.  
  42. void MyFunc()
  43. {
  44.     CDtorDemo D;
  45.     cout<< "In MyFunc(). Throwing CTest exception." << endl;
  46.     throw CTest();
  47. }
  48.  
  49. int main()
  50. {
  51.     cout << "In main." << endl;
  52.     try
  53.     {
  54.         cout << "In try block, calling MyFunc()." << endl;
  55.         MyFunc();
  56.     }
  57.     catch( CTest E )
  58.     {
  59.         cout << "In catch handler." << endl;
  60.         cout << "Caught CTest exception type: ";
  61.         cout << E.ShowReason() << endl;
  62.     }
  63.     catch( char *str )
  64.     {
  65.         cout << "Caught some other exception: " << str << endl;
  66.     }
  67.     cout << "Back in main. Execution resumes here." << endl;
  68.     return 0;
  69.  
  70. }
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. void term_func()
  7. {
  8.     //...
  9.     cout << "term_func was called by terminate." << endl;
  10.     exit( -1 );
  11. }
  12.  
  13. int main()
  14. {
  15.     try
  16.     {
  17.         // ...
  18.         set_terminate( term_func );
  19.         // ...
  20.         throw "Out of memory!"; // No catch handler for this exception
  21.     }
  22.     catch( int )
  23.     {
  24.         cout << "Integer exception raised." << endl;
  25.     }
  26.     return 0;
  27. }
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void SEHFunc( void );
  6.  
  7. int main()
  8. {
  9.     try
  10.     {
  11.         SEHFunc();
  12.     }
  13.     catch( ... )
  14.     {
  15.         cout << "Caught a C exception." << endl;
  16.     }
  17.     return 0;
  18. }
  19.  
  20. void SEHFunc()
  21. {
  22.     __try
  23.     {
  24.         int x, y = 0;
  25.         x = 5 / y;
  26.     }
  27.     __finally
  28.     {
  29.         cout << "In finally." << endl;
  30.     }
  31. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].