Exception in c ++

Source: Internet
Author: User

Exception in c ++

Errors that may occur frequently during entry:

 

this application has requested the runtime to terminate it in an unusual way. Please contact the application's support team for more information
This is because an unknown error occurs when the program is running, for example, opening a non-existent file, exploding the stack, and having a division of 0. The program directly calls the abort () function to directly terminate the program; of course, the information displayed is not necessarily the above one

 

 

In the above situation, the program solves the exception by itself. In fact, this method is very crude, and the program is terminated directly, and you do not know where the program is wrong, what's wrong? (of course, using the omnipotent cout to force tracking is also wide, haha)

Fortunately, c ++ introduces a more elegant try... catch .... the exception mechanism allows the program to handle possible exceptions by itself. This mechanism actually avoids the program from directly calling the abort () function and makes the program look more elegant. The specific structure is as follows:

 

Try {// code block that may cause exceptions} catch (exception argument) {// handle caught exceptions}
The try... catch... mechanism mainly involves the following parts:

 

1. Put codes that may cause exceptions (such as file reading) into try blocks.

2. determine when to throw an exception, that is, to add a statement to the code block that may cause an exception. throw usually uses the throw keyword to throw an exception.

3. catch: first, an exception is generated by matching the exception parameter list. If the match is successful, run the catch Block Code.

First, write a simple example.

First, no exception handling

 

#include 
 
  using namespace std;int division(int a, int b){return a / b;}int main(){int a, b;while(1){cout << Please input a and b: ;cin >> a >> b;cout << a / b =  << division(a, b) << endl;}return 0;}
 
The test result is as follows:

 



It can be seen that the program is directly ended, and the while (1)

Now let's add an exception.

 

#include 
 
  using namespace std;int division(int a, int b){if(b == 0)throw Divisor is not allowed to be 0;return a / b;}int main(){int a, b;while(1){cout << Please input a and b: ;cin >> a >> b;try{cout << a / b =  << division(a, b) << endl;cout << successful division << endl;}catch(const char* msg){cout << Error message is:  << msg << endl; }}return 0;}
 
Running result:

 



We can see that now we can know where the problem exists, and we can print the exception information. More importantly, the program can continue to execute.

As you can see, after an exception occurs, the program immediately jumps to the matching catch Block for execution, and terminates the execution of the Code in the try block.

Of course, if there is no matching exception (in the above example, it is just a simple matching string, but in fact it is more commonly used for exception object matching), then the program will also call the abort () function, then, when the compiler prompts you a bunch of error messages, your program crashes.

After reading the example above, does it feel like try and catch have a bit of real parameters and arguments?

Let's look at an example.

Now let's handle exceptions as a MyException object:

 

#include 
 
  using namespace std;class MyException{public:MyException(){msg = ;cout << in the default constructor << endl;}MyException(MyException &aa){msg = aa.msg;cout << in the copy constructor << endl;}MyException(string msg_){msg = msg_;cout << in the string constructor << endl;}string what(){return msg;}string msg;};int division(int a, int b){if(b == 0){MyException me(Divisor is not allowed to be 0);throw me;}return a / b;}int main(){int a, b;while(1){cout << Please input a and b: ;cin >> a >> b;try{cout << a / b =  << division(a, b) << endl;cout << successful division << endl;}catch(MyException &ME){cout << Error message is:  << ME.what() << endl; }}return 0;}
 
The running result is as follows:

 

You may be confused here, because in normal function parameter transfer, we pass references to avoid repeated object construction, but in try... catch... when throw throws an exception, the compiler creates a temporary object in case of a throw exception, no matter whether the catch accepts the reference transfer or the value transfer, therefore, the MyException object is constructed twice as shown in the above results, and the reference in catch is to point to this temporary object instead of the original MyException object, if you do this, throw an exception and merge it with the newly created exception object. This is more concise, so the above throws an exception and can be written

 

throw MyException(Divisor is not allowed to be 0);
I would like to talk about exception and some of his Derived classes, but it seems that there are a lot of such things on the internet, and there is nothing to talk about. Let's close the work.

 

 


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.