当前位置:网站首页>Try block and exception handling
Try block and exception handling
2022-06-25 05:13:00 【Ability with desire】
1. exception handling
1)throw expression
The error detection section uses this expression to indicate that an unhandled error has been encountered ,throw An exception condition was raised .
2)try block
The error handling section uses it to handle exceptions .try Statement block to try Keyword start , And with one or more catch End of clause . stay try The exception thrown by the code executing in the block , Usually by one of them catch Clause processing .catch Clause is also called processing code .
2) A set of exception classes defined by the standard library , Used in throw And corresponding catch Pass relevant error messages between .
2.throw expression
The system goes through throw The expression throws an exception .throw The expression consists of the keyword throw And a trailing expression .
if(!item1.same_isbn(item2))
{
throw runtime_error("Data must refer to same ISBN");
}
std:cout << item1+item2 << ::endl;
This code check ISBN Whether the objects are different , If it's not the same , Stop the execution of the program , And transfer control to the processing code that handles this error .
runtime_error Type is one of the standard library exception classes , stay stdexcept The header file defines .
3.try block
try{
program-statements
}catch(exception-specifier){
handler-statements
}catch(exception-specifier){
handler-statements
}
With keywords try Start , This is followed by a statement sequence block enclosed in curly braces .try The block is followed by one or more catch Clause .
Every catch The clause consists of three parts : keyword catch, Declaration of a single type or object in parentheses —— Called exception specifier , And a block of statements usually enclosed in curly braces .
try In the sentence program-statement Form the normal logic of the program .
while(cin >> item1 >> item2){
// Perform adding two Sales_items object
// If the addition fails ,throw One runtime_error expression
}catch(runtime_error err){
// Remind users ISBN Must match and
cout << err.what()
<<“\n Try Again?Enter y or n” <<endl;
char c;
cin >> c;
if(cin && c == 'n')
break;
}
边栏推荐
- Use js to simply implement the apply, call and bind methods
- Using JS to realize the sidebar of life information network
- Page electronic clock (use js to dynamically obtain time display)
- 滲透測試-提權專題
- buuctf(re)
- February 20ctf record
- Virtual honeypot Honeyd installation and deployment
- Detailed summary of float
- Penetration test - right raising topic
- Penetration test - directory traversal vulnerability
猜你喜欢

Compatible with Internet Explorer

基于SSH实现的学生成绩管理系统

API interface management setup -eolinker4.0

Route parameters to jump to the page and transfer parameters -- > hidden parameter list

Specific operations for uploading pictures in PHP

Prototypical Networks for Few-shot Learning

Difference between asemi high power FET and triode

The SQL response is slow. What are your troubleshooting ideas?

How to install the blue lake plug-in to support Photoshop CC 2017

Working principle of asemi three-phase rectifier bridge
随机推荐
Redis (17)
Specific operations for uploading pictures in PHP
EL & JSTL (XIII)
Click to jump out and drag the pop-up window
February 19 CTF exercise
融合CDN,为客户打造极致服务体验!
MySQL prevents Chinese garbled code and solves the problem of Chinese garbled code
ASEMI大功率场效应管和三极管的区别
Wechat applet new version prompt update
Ctfhub eggs
Critical dependency: require function is used in a way in which dependencies
How to make colleagues under the same LAN connect to their own MySQL database
CTFHub-rce
Personalized Federated Learning with Moreau Envelopes
parallel recovery slave next change & parallel recovery push change
Qdebug June 2022
Startup mode of SoC verification environment
Swift rapid development
ASEMI三相整流桥的工作原理
buuctf(pwn)