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;
}