In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces C++ exception handling example analysis, the article is very detailed, has a certain reference value, interested friends must read!
I. Background
Some exceptions are encountered when the program is running, such as:
Divide by 0;
When space is allocated dynamically with the new operator, there is not enough space to allocate it;
Subscript out of bounds when accessing array elements; file does not exist when open for reading.
These exceptions, if not detected and handled, are likely to cause the program to crash.
"Handling" can mean giving an error message and letting the program continue along an error-free path, or it can mean having to end the program but doing something necessary before it ends, such as writing data in memory to files, closing open files, freeing dynamically allocated memory space, and so on.
C++ exception handling
C++ introduces exception handling.
Exceptions provide a way to transfer program control. C++ exception handling involves three keywords: try, catch, and throw.
throw: When a problem occurs, the program throws an exception. This is done by using the throw keyword.
catch: Catch exceptions with exception handlers where you want to handle the problem. The catch keyword is used to catch exceptions.
The code in the try: try block identifies the specific exception that will be activated. It is usually followed by one or more catch blocks.
for example
#include #include using namespace std; int main () { try { throw 1; throw "error"; } catch (const char* str) { cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.