In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you an analysis of how to throw exceptions on C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
C++ language, like other programming languages, also includes exception handling. Here we will explain in detail the implementation and application of exceptions thrown by C++. I hope you can get some help to improve your understanding.
C++ throws an exception (also known as discarding exception), that is, it detects whether an exception is generated. In C++, it uses throw statement to achieve, and if an exception is detected, an exception is thrown. The format of the statement is:
Throw expression
If an exception is found in the program segment of the try statement block (including the function called in it) and the exception is discarded, the exception can be caught and handled by some catch statement after the try statement block, provided that the type of the abandoned exception matches the exception type of the catch statement. Because C++ uses data types to distinguish different exceptions, the value of the expression in the throw statement has no practical meaning when judging the exception, and the type of expression is particularly important.
Handles exceptions with a divisor of 0. In this example, the above exception with divisor 0 can be caught by try/catch statement, and throw statement can be used to realize C++ throwing exception, so as to achieve exception handling. The code is shown in the code listing.
# include / / contains the header file # include double fuc (double x, double y) / / defines the function {if (y _ blank _ 0) {throw y; / / if the divisor is 0, throw an exception} return x _ blank; / / otherwise return the quotient of two numbers} void main () {double res; try / / define the exception {res=fuc (2heli3); 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.