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

Source: Internet
Author: User
Tags try catch

I sorted out the classic triangle code circulating on the Internet and added the content I understood.

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.

[Cpp]
// AbnomalTest. cpp: defines the entry point of the console application.
//
 
# Include "stdafx. h"
# Include <iostream>
# Include <afx. h>
Using namespace std;
 
 
Class triangle // class that defines a triangle
{
Public:
Float a, B, c, d; // a, B, c, and Helen formula constant d
Float s; // Triangle Area
Public:
Triangle (){}
Triangle (float a1, float b1, float c1)
{
A = a1;
B = b1;
C = c1;
}
// Determine if it is a triangle
String judgment ()/* throw (string) The exception specification */
{
String temp;
Int TempNum;
If (a + B) <c | (a + c) <B | (c + B) <)
{
TempNum = 9;
Temp = "not a triangle ";
Wcout <temp. c_str () <endl;
 
Throw (TempNum );

}/* Throws with throw instead of visual output on the console. Instead, catch is used for subsequent processing */
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 (, 3); // only pass the value, initialize
Try
{
A. judgment ();
A. dimension ();
Cout <"the area of Triangle a is:" <a. s <endl;
}
// Catch (string & TempErr) // Yes, I accept it. 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, as if two cannot be used at the same time
// 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;
}
 

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.