Quickly uses the C ++ exception capture mechanism (several lines of code in a program)

Source: Internet
Author: User

Sorted out classic triangles circulating on the InternetCode, Added content that you understand.

The final goal is to first use try catch blocks in C ++. Then I thoroughly understood the error mechanism of C ++.

Here, I would like to share with you what is wrong with writing, or what is wrong with writing,

You are welcome to raise any suggestions for better improvement.

I would like to be grateful here.

// Abnomaltest. cpp: defines the console application Program . // # Include "stdafx. H "# include <iostream> # include <afx. h> using namespace STD; Class triangle // defines the class of a triangle {public: float a, B, c, d; // triangle three sides a, B, c, helen formula constant dfloat s; // Triangle Area public: triangle () {} triangle (float A1, float B1, float C1) {A = A1; B = b1; C = C1;} // judge whether the triangle string judgment ()/* Throw (string) is called the exception specification */{string temp; int tempnum; if (a + B) <c | (A + C) <B | (C + B) <A) {tempnum = 9; temp = "not a triangle "; wcout <temp. c_str () <Endl; throw (tempnum);}/* Throw with throw. It is not visible output on the console, but is followed by catch, then process */else {/* locale LOC ("CHS"); wcout. imbue (LOC); */temp = "Triangle"; cout <temp. c_str () <Endl; throw (temp) ;}} void dimension () // calculated area {d = (A + B + C)/2; // Helen formula S = SQRT (D * (d-a) * (D-B) * (D-C) ;}}; int _ tmain (INT argc, _ tchar * argv []) {Triangle A (7,2, 3); // only pass the value, initialize try {. judgment ();. dimension (); cout <"area of Triangle A:" <. S <Endl;} // catch (string & temperr) // Accepted. Accepted. Please note that I have defined a string to accept it. // {// Either use the Catch Block defined by myself or use the omnipotent Catch Block. It seems that the two cannot be used simultaneously. // wcout <temperr. c_str () <Endl; //} catch (...) {/* locale LOC ("CHS"); // either use this block output or use another output wcout. imbue (LOC); wcout <L "Universal capture" <Endl; */cout <"Universal capture" <Endl ;}return 0 ;}

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.