assertと異常の使用

1687 ワード

  • assertはdebugモードで使用でき、#define NDEBUGでassertが機能しない
  • try
  • {
  • throw
  • }
  • catch
  • {
  • }
  • 三者欠一不可10.throwが投げ出すのは基本タイプであってもよいし、カスタムタイプ
  • であってもよい.
  • #include#define NDEBUG//assert断言を無効にし、assertはreleaseの下で機能しない#include#include using namespace std;class Student {

  • }; double devide(double x, double y) { if (y == 0) { throw y; } return x/y; } class myERROR { public: myERROR(string str):myerror(str){} string what() { return myerror; } private: string myerror; }; class A { public: explicit A(int i=5,int j=10) { num = i; mb = j; } void display() { cout << num< } private: int num; int mb; }; void coutMy(const char*ch)/string&chを使用できません{cout<try { double res = devide(2, 3); cout << res << endl; res = devide(4, 0); cout << res << endl; } catch (...) { cerr << "error of diviing zero.
    "; //exit(1); } cout << "----------------------" << endl; int x = 0; try { if (x == 0) { throw myERROR("x===0"); } } catch (myERROR &myserror) { cout << myserror.what() << endl; } cout << "----------------------" << endl; A a; //a = 100;y explicit, a.display(); cout << "----------------------" << endl; coutMy("789456"); cout << "----------------------" << endl; system("pause"); return 0;

    }