Lesson 64 - exception handling in C + + (I)

Keywords: Programming

1, C + + exception handling

C + + has built-in exception handling syntax element try..catch

  • try statement handles normal code logic
  • catch statement handling exception
  • Exceptions in a try statement are handled by the corresponding catch statement
try{
  double r = divide(1,0);
}
catch(...){
  cout << "Divide by zero.." << endl;
}

C + + throws exception information through throw statement

double divide(double a ,double b){
  const double delta = 0.00000000000001;
  double ret = 0;
  if(!((-delta < b) && (b < delta))){
    ret = a / b;
  }
  else{
    throw 0;    //Exception of division 0 generated
  }
  return ret;
}

Analysis of C + + exception handling

  • throw exception must be handled by catch
    • The current function can handle exceptions, and the program continues to execute
    • If the current function cannot handle the exception, the function stops executing and returns

Unhandled exceptions travel up the function call stack until they are processed, otherwise the program stops executing.

Programming experiment: a preliminary study of C + + exception handling

#include<iostream>
using namespace std;

double divide(double a,double b){
    const double delta = 0.000000000000001;
    double ret = 0;
    if(!((-delta < b) && (b < delta))){
        ret = a / b;
    }
    else{
        throw 0;
    }
    return ret;
}

int main(int argc,char* argv[]){
    try{
        double r = divide(1,0);
        cout << "r = " << r << endl;
    }
    catch(...){
        cout << "Divide by zero..." << endl;
    }
    return 0;
} 

Print results:

Divide by zero...

The same try statement can keep up with multiple catch statements

  • The catch statement can define the exception types to be handled
  • Different types of exceptions are handled by different catch statements
  • Any type of exception can be thrown in a try statement
  • catch(...) is used to handle all types of exceptions
  • Any exception can only be caught once

Matching rules for exception handling:

Programming experiment: exception type matching

#include<iostream>
using namespace std;

void Demo1(){
    try{
        throw 'c';
    }
    catch(char c){
        cout << "catch(char c)" << endl;
    }
    catch(short c){
        cout << "catch(short c)" << endl;
    }
    catch(double c){
        cout << "catch(double c)" << endl;
    }
    catch(...){
        cout << "catch(...)" << endl;
    }
}

void Demo2(){
    throw string("D.T.Software");
    //throw ("D.T.Software"); 
}

int main(){
    Demo1();
    try{
        Demo2();
    }
    catch(char* s){
        cout << "catch(char *s)" << endl;
    }
    catch(const char* cs){  //Print corresponding to line 24
        cout << "catch(const char *cs)" << endl;
    }
    catch(string ss){
        cout << "catch(string ss)" << endl;
    }
    return 0;
}

Print results:

catch(char c)
catch(string ss)

Two, summary

  • The concept of directly supporting exception handling in C + +
  • try...catch... Is a special statement for exception handling in C + +
  • try statement handles normal code logic, catch statement handles exception
  • The same try statement can keep up with multiple catch statements
  • Exception handling must be strictly matched without any type conversion

Posted by keenlearner on Tue, 31 Mar 2020 02:16:16 -0700