Parse C++ exception handling

Author: Conscience Stills

Reporting Authorization and Outlook: Welcome to add WeChat: Allen-Iverson-me-LYN

executive summary

There are often problems encountered during the running of a program, such as array subscripts crossing bounds, dividing by zero, etc. If these errors are not found and handled, our code is likely to cause the program to crash.So we generally make predictions about errors that could cause the program to crash, both C and C++.

However, C's judgment code should be close to, or even tightly coupled with, the location where the related function is executed.C++ introduces error handling, which allows execution programs to be separated from error detection code, so that we can safely write code separately, call a function multiple times, and write error handling code only in one place.

 

Exceptions provide a way to transfer program control, with several keywords related to C++ exception handling:

  1. Throw: When a problem occurs, the program throws an exception flag, which is done by the throw keyword.

  2. Catch: Catches exceptions through exception handlers where we deal with problems.The catch keyword is used to catch exceptions.

  3. Try: The code in the try block identifies the specific exception that will be activated.It is usually followed by one or more catch blocks.

1. throw

throw

Use the throw statement to throw an exception anywhere in the code block.The operand of a throw statement can be any expression, and the type of result of the expression determines the type of exception thrown.You can throw a variable or a function

void C(int i){    int    a =1;    double b =1.2;    float  c = 1.3;    if(i ==0)    {        cout<<"in C ,it is OK"<<endl;    }    else if(i==1)    {        throw a;    }    else if(i==2)    {        throw b;    }     else if(i==3)    {        throw c;    }    else if(i==4)    {        throw Myexception();    }    else if(i==5)    {        throw MySubexception();    }      }

 

2. try and catch

 

Catch Exceptions

A catch block follows a try block and is used to catch exceptions.We can specify the type of exception we want to catch, which is determined by the exception declaration in parentheses after the catch keyword.

void A(int i){    try{        B(i);    }catch(int t)    {        cout<<"catch int excepting  "<<t<<endl;    }catch(double d)    {        cout<<"catch double excepting  "<<d<<endl;    }catch(MySubexception &e){        e.waht();    }catch(Myexception &e){        e.waht();    }    catch(...)    {        cout<<"catch other excepting  "<<endl;    }}

If you want the catch block to handle any type of exception thrown by the try block, you must use an ellipsis within the parentheses of the exception declaration..., as follows:

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

Where an error may occur, put the code in the try and catch it.The snippet that occurs defines the error code with the throw command.

In the event of an exception, interrupt program execution and go directly to catch.When capturing, capture a small area first, then a large area.

 

3. Examples of testing

 

#include <iostream>#include <string.h>#include <unistd.h>#Include <stdlib.h>/* Throw an integer floating point etc. through the throw command, then catch*/use namespace std; class Myexception {public: void waht(void){cout<<"this is Myexception"<"endl;}; MySubexception: public Myexception {public: void waht(void){cout<<"this is MySubexception"<<endl;}; void C(int i){an =1;Double B =1.2; float C = 1.3; if (i ==0) {cout<< "in C, it is OK"< endl;} else if (i==1) {throw a;} else if (i==2) {throw b;} else if (i==3) {throw c;} else if (i==4) {throw Myexception ();} if (i= 5) {throw MySubexception ();}} void B(int i) {cout<<"call C..."< endl; C(i); cout<<"After call C..."< endl;} void A(int i) {try{B(i);} catch(int t) {cout<<"catch int excepting"<"t<< endl;} catch(double d) {cout<<"catch excepting"<"d<< endl;} (Myexception &e){e.waht}catch (Myexception &e) {e.waht ();} catch (...)} {catch (...) {cout<<< "catch other excepting" <endl;}}}} int main (int argc, char** argv) {int i; if (argc!=2) {cout<<<<< "Usage:"<endl; cout<argv [0]<<<<<<< < < 0|1|2|3> "< <endl; return -1;} I = STI = ststi = < (< 0 | ||| | 1 ||| |2 | | | | | | 3 0; /* converted to integer */ A(i); return 0;}

 

This is the C++ exception handling that I shared. The code inside has been practiced. If you have any better ideas, please share with us.

 

Share more, Scavenger pays attention to me

WeChat: Allen-Iverson-me-LYN

 

 

×
12/100
                         
Article Tags:
c++ Add Article Tags
Category Column:
C/C++
New Category Column
Select up to three categories columns#for secondary categories
C/C++
Linux
Internet of Things
ST
Win
ARM
Embedded
IDE
cortex
ROS
Informal essay
IAR
Code Upgrade
vs code
eclipse
MDK
DEBUG
CubeMX
Mozilla Public License
version control
git
Resource information:
Article Type:
Please select Original Reproduction Translation*
Apply for originality will be enabled Creative Commons Copyright templates, if not original articles, please choose to reprint or translate.Original articles are rewarded by default. Reward management
Publication Form:
VIP visible to public and private fans Revenue Center
Warm Tip: Do not post anything related to politics, advertising, marketing, wall-turning, violation of national laws and regulations, etc. See CSDN Resource Sharing Specification for details
Publish Blog Return

Tags: C++ Mobile Linux Eclipse git

Posted on Thu, 14 May 2020 23:59:30 -0400 by tlenker09