当前位置:网站首页>Exception handling mechanism topic 1
Exception handling mechanism topic 1
2022-07-25 16:36:00 【Dry rice Xiaobai】
1. Exception is a program control mechanism , Independent and complementary to functional mechanisms
- Function is a program control system connected by upper and lower functions expanded in stack structure .( System stack , Function call model )
- Another control structure when abnormal , It is attached to the stack structure , You can set multiple exception types as capture conditions at the same time , So as to jump back in the stack mechanism with type matching
2. Abnormal design purpose
Stack mechanism is a highly rhythmic control mechanism , Object oriented programming requires direction between objects 、 Purposeful control transmission . from the beginning , Exception handling is aimed at changing the program control structure , To adapt to the theme that object-oriented programs work more effectively , Not just for error handling .
After the abnormal design , But I find that I get the most benefit from error handling
The basic idea of exception handling
Traditional exception handling mechanism : Handle errors by returning values In the same function 【 A typical :C Language , Some functions of system library 】
C++ exception handling :
1.C++ Exception handling mechanism of So that the exception throwing and exception handling do not have to be in the same function in , The underlying function can focus on solving the specific problem , You don't have to think too much about exception handling . The caller of the upper layer can design the handling of different types of exceptions in the appropriate place .【 It realizes the separation of exception handling and exception occurrence 】
2. Exception is specially for a series of error handling in abstract programming ,C++ Function mechanism cannot be used in , Functions are based on stack structure , The stack structure is characterized by first in and last out , Sequential access , Don't jump in line , But the characteristic of error handling is that when you encounter error information, you want to go to several levels and try again .
3. Abnormally detached from the functional mechanism , Determines its leap back to the function
4. Exception spanning function
Exception basic grammar
Program means of throwing exceptions : void fun() { throw expression ; } | Catch and handle exceptions try { .... } catch( Exception type declaration ) ... catch( type ( Parameters )) .... |
1) If there are exceptions, pass throw Operation creates an exception object and throws
2) Embed program segments that may throw exceptions in try In blocks . Control arrives through normal smooth execution try sentence , And then execute try Protection section in the block
3) If no exception is caused during the execution of the protection section , So follow me try After the block catch Clause does not execute . The program from try The last one following the block catch The statement after Clause continues to execute
4)catch Clause in try The order of occurrence after the block is checked . Matching catch Clause will catch and handle exceptions ( Or continue to throw exceptions )
5) If the matching processor cannot be found , be Operation function terminate Will be automatically called , Its default function is to call abort To terminate the program
6) Can't handle the exception , Can be in catch Of The last branch , Use throw grammar , Throw it up
Procedure cases

terminate Call situation
#include <iostream>
using namespace std;
class A{};
class B{};
void f()
{
int a = 0;
if(a == 0) throw A();
}
void g()
{
try{
f();
}
catch(B e){
cout<<"exception B\n"<<endl;
}
}
int main(void)
{
g();
return 0;
}throw A Will penetrate the function f,g,main. Arrive at the last line of defense of the system ---- inspire terminate function . This function calls abort Function terminates the operation of the program .
The function of the last line of defense can be set by the programmer , So as to stipulate its behavior before termination
Modify the system default behavior
adopt set_terminate Function modification cannot catch the default handler of exceptions , Thus, when an unexpected exception occurs , Handled by custom functions .
void myTerminate(){...}
set_terminate(myTerminate); // Its parameter is void(*)()
1. Constructor has no return value , Cannot report running status by return value , So only through a non functional mechanism , That is, the exception mechanism to solve the error problem of the constructor
2. Exception mechanism and function mechanism do not interfere with each other , But the way of capturing is based on type matching . Catch a match equivalent to the return type of a function , Instead of matching function parameters , Therefore, the capture does not need to consider the problem of multiple data type matching in a throw
#include <iostream>
using namespace std;
class A{};
class B{};
int main(void)
{
try
{
int j=0;
double d=2.333;
char *p = "aaaaaaaaaaaaaaaa";
int key =0;
cin>>key;
switch (key)
{
case 1:
throw j;
break;
case 2:
throw d;
break;
case 3:
throw p;
break;
case 4:
throw A();
break;
case 5:
throw B();
break;
default:
break;
}
}
catch(int)
{
cout<<"int exception\n"<<endl;
}
catch(double)
{
cout<<"double exception\n"<<endl;
}
catch(char*)
{
cout<<"char* exception\n"<<endl;
}
catch(A)
{
cout<<"class A exception\n"<<endl;
}
catch(B)
{
cout<<"class B exception\n"<<endl;
}
return 0;
}catch The code must appear in try after , And in try After the block, there can be multiple catch Code block , To capture various types of throws . The exception mechanism is based on this principle : The essence of program operation is that data entities are doing some operations ( There is another sentence to describe , There are only two parts in the program, one is the code area and the other is the data area ), Therefore, where abnormal phenomena occur , There must be something wrong with some entity , The data type corresponding to the entity is used as the basis for throwing and capturing .
Exception capture matches strictly by type ( The severity matches the type of template )
边栏推荐
猜你喜欢

百度富文本编辑器UEditor单张图片上传跨域
![Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]](/img/b5/5c7fc70b8025cf7ef21d645a3ac22e.png)
Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]

用递归进行数组求和

easyui datagrid控件使用

Food safety - do you really understand the ubiquitous frozen food?

Win11桌面切换快捷键是什么?Win11快速切换桌面的方法

复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
![[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code](/img/9e/138e4b160fa9bd6486fac44a788d09.png)
[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code

7.依赖注入

如何安装govendor并打开项目
随机推荐
Which led display manufacturer is better
152. Product maximum subarray
Win11自带画图软件怎么显示标尺?
链游开发现成版 链游系统开发详细原理 链游源码交付
新增批量删除
MySQL之联表查询、常用函数、聚合函数
【obs】转载:OBS直播严重延迟和卡顿怎么办?
Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载
【读书会第13期】+FFmpeg视频采集功能
MyBaits
百度富文本编辑器UEditor 图片宽度100%自适应,手机端
Talk about how to use redis to realize distributed locks?
MySQL视图
【ZeloEngine】反射系统填坑小结
如何安装govendor并打开项目
柏睿数据加入阿里云PolarDB开源数据库社区
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
easyui入门
[cloud co creation] explore how gaussdb helps ICBC create core financial data
LVGL 7.11 tileview界面循环切换