0831-Class vs. Object exception/Interview 100 questions 1-100

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.