1 #include2 #include 3 using namespace std; 4 5 //继承自带的异常 6 class sizeerror :public exception 7 { 8 public: 9 sizeerror() :exception("尺寸大小异常")10 {11 12 }13 14 const char *what()15 {16 char *p = "尺寸大小异常";17 cout << "尺寸大小异常" << endl;18 return p;19 }20 };21 22 class print3d23 {24 public:25 print3d(double size)26 {27 if (size > 100)28 {29 throw &sizeerror();30 }31 }32 };33 void main()34 {35 try36 {37 print3d p(300);38 }39 catch (sizeerror *p)40 {41 p->what();42 }43 cin.get();44 }