c + + exception handling

When designing a program, usually the program will also make mistakes. Program errors can be divided into three types: syntax errors, logic errors and runtime errors

Syntax errors occur during the compile and link phases

Logical error is that we have a problem with the thinking of the code we are writing, and we can't meet the requirements. Solve through debugging

Debugging method: View stack information by printing information - printf or by IDE debugging trace

Runtime error refers to the error that occurs during the running of the program. C language --- perror

In C + +, in order to run and maintain better, the exception mechanism is introduced, so that we can catch runtime errors and give the program a last chance to speak

1 capture exception

In C + +, it is possible to catch exceptions with the help of exception mechanism

1. throw ---- throw or throw the exception to the caller

2. try{

        // A statement executed normally, but an exception statement may be thrown during execution

        }catch(exceptionType     val)

        {

        // Statement handling exception

         }

matters needing attention:

1.try and catch are both C + + keywords followed by statement blocks. You cannot omit {}

2. If the exception does not occur, it will not be detected, and the catch will catch the exception. The statement will not execute. 3. The exception type Val type should be consistent with the type of the thrown error

2 use of exception handling

The try statement is normal code logic. When the try statement is abnormal, throw the exception through the throw statement and exit the try statement
, the catch statement handles exceptions
  1 when throw statement throws an exception, it will directly jump to catch statement processing
  2. Catch statements are allowed to be overloaded. Multiple catch statements can be followed by try statements, but the order should be the same as thorw
  3 different types of exceptions are captured by different catch statements, and the order is strictly matched from top to bottom without implicit conversion
  4. Catch (...) statement, which is equivalent to else statement, means that all undefined exceptions are caught and can only be placed in the
  There is the end of catch  

class Test
{

public:
    void printError()const
    {
        cout << " error -----" <<endl;
    }
};
void func()
{
   // throw 1;// If the type of error thrown is int, it is handled by the catch statement of int type
    //throw 1.0; // If the type of error thrown is double, it is handled by the catch statement of double type
    //throw 1.5f;// If the type of error thrown is float, the catch statement of float type will handle it
    //throw "abc"; // Captured by a catch statement of char const * type, string type
    //throw string("hello");// Captured by a catch statement of type string
    //throw class type
    throw Test(); //throw new Test()

}
int main()
{
    try {
        func();
    } catch (int e) {
        cout << "error:" << e<<endl;
    }catch(double e){
         cout << "error:" << e<<endl;
    }catch(float e){
        cout << "error:" << e<<endl;
   }catch(char const* e){
        cout << "error:" << e<<endl;
   }catch(string e){
        cout << "error:" << e<<endl;
   }catch(const Test& e){

        e.printError();

    }catch(...){
        cout << "other type error"<<endl;
   }

    return 0;
}

3 exception class in C + +

/**
   *  @brief Base class for all library exceptions.
   *
   *  This is the base class for all exceptions thrown by the standard
   *  library, and by certain language expressions.  You are free to derive
   *  your own %exception classes, or use a different hierarchy, or to
   *  throw non-class data (e.g., fundamental types).
   */
  class exception
  {
  public:
    exception()  { }
    virtual ~exception() ;

    /** Returns a C-style character string describing the general cause
     *  of the current error.  */
    virtual const char what() const ;
  };

Keywords: C++

Added by morphboy23 on Tue, 14 Sep 2021 03:35:10 +0300