class-To-object exceptions
CPP Exception
#include <iostream> #include <string.h>using namespace std;//identifies the wrong type class wrong {};int intdiv (int A, int b) {try{ if (b==0) {Throw 10;//can be no matter what object wrong ();} int c = A/b;return C;} catch (int data)//type name {cout << "division exception already handled"; return-1;}} int Intdiva (int a, int b) {return a/b;} void Main () {int x, y;cin >> x >> y;try{if (y==0) {throw "is divisor 0";} else if (x==0) {throw "divisor is 0";}} catch (const char * s) {if (strcmp (S, "divisor is 0") ==0) {cout << "is divisor 0 exception, please enter again"; Cin >> x >> y;} else if (strcmp (S, "divisor is 0") = = 0) {cout << "divisor is 0 exception, please enter again"; Cin >> x >> y;}} Std::cout << intdiv (x, y); Cin.get (); Cin.get (); Cin.get ();}
Exception of Class
#include <iostream>using namespace Std;class wrong{};class wronga{};class array{public:array (int num) {n = num;if ( num<=0) {throw wrong ();} p = new int[num];//The correct code after the throw. Will not be run. for (int i = 0; i < num;i++) {P[i] = 0;}} int & operator[] (int num) {if (num < 0 | | num>= N) {throw Wronga ();} return p[num];} Protected:private:int *p;int N;}; void Main () {Try{array myarrar (2); myarrar[-1];} catch (Wronga e) {cout << ' subscript out of bounds ';} catch (wrong e) {cout << "program has an exception, the array size must be greater than or equal to 1";} Cin.get ();} void MainA () {int a[3] = {1, 2, 3};//printf ("%d", 2[a]),//* (2+a)//printf ("%d", a[9886655]); GetChar ();}
#include <iostream> #include <string>using namespace std;class box//cube {public:box (int data) {cout << "start construction"; F (Data ==0) {Zero z1;z1.errorcode = 212;throw Z1;} else if (data >0 && data<100) {throw small ();} else if (data>10000) {throw big ();} else if (data>100 && data<10000) {a = data;} Else{throw wrong{};}} int Gettiji () {return a*a*a;} Class Zero{public:int errorcode;}; Class Wrong{};class Big{};class small{};p rivate:int a;//variable length};void main () {Try{box newbox (0);} catch (Box::zero W) {if (w.errorcode==22) {cout << "error 22nd, cube length cannot be 0"; } else {cout << "fei22 error cube length cannot be 0"; }}catch (box::wrong) {cout << "cube length anomaly";} catch (Box::big) {cout << "cube length too long";} catch (Box::small) {cout << "cube length Taiduan";} Cin.get ();}
interview question 1-1 xx
Organize documents separately
0831-Class vs. Object exception/Interview 100 questions 1-100