Exception exception in C + +

Source: Internet
Author: User

Errors that can often be encountered when getting started:

This application have requested the runtime to terminate it on an unusual. Please contact the application's support team for more information
This is because the program at run time an unknown error occurred, such as: open a nonexistent file, burst stack, the divisor is 0 and other errors, the program directly call the Abort () function to terminate the program directly; Of course, the information displayed is not necessarily the above


The above situation is the program itself to solve the problem of the exception, this way is actually very rude, directly terminate the program run, and you do not know where the program is wrong, because of what is wrong (of course, with the almighty cout force tracking is also wide to drip, haha)

Fortunately, C + + introduced a more elegant try ... catch .... Exception mechanism to allow the program ape to handle the possible exception itself, this mechanism actually avoids the program calls the Abort () function directly, makes the program look more elegant, the concrete structure is as follows:

try{//code block that may throw an exception}catch (Exception argument) {//Handle caught exception}
Try...catch ... The mechanism is actually mainly the following parts:

1. Place code that might throw exceptions (e.g., file reads, etc.) into a try block

2. Determine when to throw an exception, that is, a block of code that might throw an exception that needs to be thrown by an exception, and throws an exception usually using the Throw keyword

3.catch matches the exception generated by the exception parameter list first, and if the match succeeds, the code that executes the CATCH block

Let's write a simple example

First, there's no exception handling.

#include <iostream>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;}
Test it casually, the result is as follows:



Can see the program is directly over, and ignore that while (1)

Well, let's add a little bit of anomaly.

#include <iostream>using namespace Std;int Division (int A, int b) {<span style= "White-space:pre" ></span >if (b = = 0) <span style= "White-space:pre" ></span>throw "Divisor is not allowed-be 0"; <span style= "whi Te-space:pre "></span>return A/b;} int main () {<span style= "White-space:pre" ></span>int A, B;<span style= "White-space:pre" ></span >while (1) {<span style= "white-space:pre" ></span>cout << "please input A and B:"; <span style= " White-space:pre "></span>cin >> a >> b;<span style=" White-space:pre "></span>try{ <span style= "White-space:pre" ></span>cout << "A/b =" << Division (A, B) << Endl;<span s Tyle= "White-space:pre" ></span>cout << "successful division" << Endl;<span style= "White-space :p Re "></span>}catch (const char* msg) {<span style=" white-space:pre "></span>cout <<" Error Message is: "<< msg << endl; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" >< /span><span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span> return 0;}
Operation Result:



You can see now we can know where the problem is, and can print out the exception information, more importantly, the program can still continue to execute

As you can see, after an exception is generated, the program jumps immediately to the matching catch block execution, and the execution of the code in the try block is terminated

Of course, if there is no match on the resulting exception (the above example is just a simple matching string, actually more commonly used is the exception object matching), then the program will also call the Abort () function, and then the compiler gives you a bunch of wrong information, your program is dead

Looking at the above example, is not the sense that the try and catch have a little argument and a type parameter

Well, let's look at an example.

Let's do it now. The exception is treated as a MyException object:

#include <iostream>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 results of the operation are as follows:

Everyone here may wonder, because in normal function parameter passing, we pass the reference to avoid constructing the object repeatedly, but in the try...catch ... is not the same, throw when throwing an exception, whether the catch is to accept the reference pass or accept the value of the pass, the compiler will throw the exception when a new temporary object, so there will be shown in the results above the two constructed MyException object, and the reference in the catch is just a pointer to the temporary object, not the original MyException object, all right? Throwing exceptions merges with the new exception object, which is more concise, so the above throw exception can be written as

Throw MyException ("Divisor is not allowed-be 0");
I would also like to talk about exception and some of his derivative classes, but it seems that there are many of these things on the internet, there is nothing to talk about, it's over.

Everyone is interested in here to understand the C + + comes with the standard exception class, are some form of things, not difficult to drop ~ ~




Exception exception in C + +

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.