C ++ Exception Handling ZZ
Exceptions usually occur when the program is difficult to detect and the operation is abnormal, for example, Division by 0 or array out-of-bounds access.
The exception handling syntax is usually
Try
{
Throw ()
}
Catch ()
{
}
The keyword try and the content in braces after it are called try blocks. Simply put, it contains the places where errors may occur (that is, the places we want to detect ). When we detect an error message, the keyword throw will throw the error (strong). What should we do? Where can I leave it? Our catch keyword solves this problem for us. Catch is usually used to receive exceptions thrown by throw. Note that the type thrown by throw must match the type accepted by catch. Just like a number 5 can only be attached to an int-type A (of course, Int-type B can also be... it seems that there are Bricks flying ...)
Next, let me learn about the three usage methods of exception handling:
1: Common Exception Handling
Let's take a look at the following code:
# I nclude <iostream>
# I nclude <string>
# I nclude <math. 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;
}
Void judgment () // determines if it is a triangle
{
String Yorn;
If (a + B) <c | (A + C) <B | (C + B) <A) // any side and smaller than the third side are not triangles
{
Yorn = "not a triangle ";
Throw (Yorn );
}/* If any two sides and smaller than the third side, we think it is an exception (not a triangle, what does it mean for me ??), Throw. Note that we define a string-type variable Yorn (yes or no) and throw it. The string type is also used for receiving. The following figure shows ~~ */
}
Void dimension () // calculated area
{
D = (A + B + C)/2; // Helen Formula
S = SQRT (D * (d-a) * (D-B) * (D-C ));
}
};
Void main ()
{
Triangle A (7,2, 3 );
Try
{
A. Judgment ();
A. dimension ();
Cout <"the area of Triangle A is:" <A. S <Endl;
}
Catch (string & ERR) // accepted, accepted. Please note that I have defined a string to accept. So can I define an int or another type? Yes, it is completely acceptable, but it cannot be accepted :)... There are Bricks flying... */
{
Cout <err <Endl;
}
}
Of course, C ++ also provides a 10 thousand-performance receiver,
You can also write it like this.
Catch (...)
{
Cout <"wrong, buddy. Do you know what a triangle is? "<Endl;
}
Catch (...) it's a 10 thousand-performance receiver (don't ask me why, the C ++ rule is like this, just as you don't ask me why I want to write this article, it's late, i'm still writing... write... sorry, it's a long journey. Pull it back .)
The universal receiver can also be used with the catch I wrote. For example, you can write it again:
Try
{
A. Judgment ();
A. dimension ();
Cout <"the area of Triangle A is:" <A. S <Endl;
}
Catch (string & ERR)
{
Cout <err <Endl;
}
Catch (...)
{
Cout <"wrong, buddy. Do you know what a triangle is? "<Endl;
}
The universal receiver should be written at the end. The last one of all catch is either the catch () You defined (no omnipotent) or the catch (...)
If you have to write it somewhere else, then .. Rewrite the compiler.
2: exception Specification
When I look at the member function void judgment () in the class ()
{
String Yorn;
If (a + B) <c | (A + C) <B | (C + B) <)
{
Yorn = "not a triangle ";
Throw (Yorn );
}
}
I am in a surging mood and cannot be calm. Write throws and writes. I want to write two. delete one and leave one... Wait, two throws. Yes, I can write 200 throws, But what type should I throw? At present, it seems that I only want to throw something of the string type. What should I do if I throw an error quickly !). OK. Check the information and check if there is any solution ...)
I am not happy with the effort. I have turned out the exception specification syntax. Let me see how it is implemented.
Or the appeal member function, rewritten
Void judgment () Throw (string)
{
String Yorn;
If (a + B) <c | (A + C) <B | (C + B) <)
{
Yorn = "not a triangle ";
Throw (Yorn );
}
}
In this way, I can only throw an exception of the string type.
The throw () statement is an exception specification between the function parameter list and the function body.
The compiler ensures that exceptions other than types contained in throw () cannot be thrown.
For example, we only want to throw an int or string exception in the function.
We can write our functions in this way.
Viod myfun (int A, string B) Throw (INT, string)
{
// Function implementation
......
Throw (INT type)
Throw (string type)
Throw (char type) // do you want to throw it? It is estimated that it cannot be throttled ~~~~
......
}
In this way, we can only throw an int or string exception in the function.
3: exceptions and inheritance
View code first
# I nclude <iostream>
# I nclude <string>
# I nclude <math. h>
Using namespace STD;
Class triangle: Public exception // class that defines a triangle
{
Public:
Float a, B, c, d;
Float S;
Public:
Triangle ()
{}
Triangle (float A1, float B1, float C1)
{
A = A1;
B = b1;
C = C1;
}
Void judgment () Throw (exception) // you can check whether a triangle exists.
{
If (a + B) <c | (A + C) <B | (C + B) <A) // any side and smaller than the third side are not triangles
{
Throw exception ("not a triangle, play with me ");
}
}
Void dimension () // calculated area
{
D = (A + B + C)/2; // Helen Formula
S = SQRT (D * (d-a) * (D-B) * (D-C ));
}
};
Void main ()
{
Triangle A (7,2, 3 );
Try
{
A. Judgment ();
A. dimension ();
Cout <"the area of Triangle A is:" <A. S <Endl;
}
Catch (exception & E)
{
Cout <E. What () <Endl;
}
}
The appeal code is written in the inheritance of exceptions. The triangle inherits from the category Rion class, and the category Rion is a standard exception class in the C ++ class library. The triangle class inherits the exception handling, for example, the statement throw exception ("not a triangle, play with me"); here, an exception of the exception type is thrown. We need to use catch (exception & E) When accepting the exception) and an exption method is used to input the exception description.
We can also write an exception handling class by ourselves without the exception class in the class library.
The following is a very simple exception handling class I wrote. I use it as a base class and use triangle to inherit it.
# I nclude <iostream>
# I nclude <string>
# I nclude <math. h>
Using namespace STD;
Class Exec
{
PRIVATE:
String error;
Public:
Exec ()
{}
Exec (string ERR)
{
Error = err;
}
String geterr ()
{
Return Error;
}
};
Class triangle: Public exec // class that defines a triangle
{
Public:
Float a, B, c, d;
Float S;
Public:
Triangle ()
{}
Triangle (float A1, float B1, float C1)
{
A = A1;
B = b1;
C = C1;
}
Void judgment () Throw (EXEC) // determines if it is a triangle
{
If (a + B) <c | (A + C) <B | (C + B) <A) // any side and smaller than the third side are not triangles
{
Throw (Exec ("not a triangle, you play with me "));
}
}
Void dimension () // calculated area
{
D = (A + B + C)/2; // Helen Formula
S = SQRT (D * (d-a) * (D-B) * (D-C ));
}
};
Void main ()
{
Triangle A (7,2, 3 );
Try
{
A. Judgment ();
A. dimension ();
Cout <"the area of Triangle A is:" <A. S <Endl;
}
Catch (Exec & E)
{
Cout <E. geterr () <Endl;
}
}
The exec class can be said to be an exception handling class (I wrote it for exception handling), and the triangle inherits from it. After I run it, the effect is the same as that of exception. But I wrote it, ah ~ Don't mention it (I have to post it like this, Suo, you can block the bricks for me ).
The above is my little learning experience on Exception Handling. Comrades, step on my shoulder. Let's take the bricks to others. After N years of learning, I learned a computer for N years and thought I was still familiar with C ++ ~ I learned from suo's class that I don't know anything about C ++. Comrades, please.
Late at night, sleeping...
I am very grateful for the comments, modifications, and shortcomings in this article ~~