C + + try throw catch
These three are used together,
- Try {
- Statement list;
- }
- Catch(TypeA arg ) {
- Statement list;
- }
- Catch(TypeB arg ) {
- Statement list;
- }
- . . .
- Catch(Typen arg ) {
- Statement list;
- }
The throw is placed in a try, and if the throw is executed, the corresponding catch is captured and the corresponding value can be caught. Examples are as follows
- #include <exception>
- #include <stdexcept>
- #include <iostream>
- using namespace Std;
- struct Argument_error : public std:: Runtime_error
- {
- Argument_error(const STD::string& S): Std:: Runtime_error(S) { }
- };
- int main(int argc)
- {
- STD:: set_terminate(__gnu_cxx:: __verbose_terminate_handler);
- Try{
- if (argc > 5)
- Throw Argument_error("ARGC is greater than 5!" );
- Else
- Throw argc;
- }
- Catch (Argument_error e){
- cout << "Argc:others"<< Endl;
- }
- Catch (int e){
- cout << "ARGC:" << E << Endl;
- }
- }
Reference article ==============================c++ Try_catch 1, Basic introduction
Try
{
Exception thrown in program
throw value;
}
catch (ValueType V)
{
Exception handler Segment
}
Syntax Summary: Throw throws a value, catch accepts, of course, the throw must be valid in a try statement block.
2, in-depth throw:
(i), after the program accepts the throw statement, the destructor is automatically called, the domain (in parentheses after the try) object is clean, and then the
Into the Catch statement (if you exit the loop in the loop body).
This mechanism can cause some fatal errors, such as when a "class" has a pointer member variable (again a pointer!). ), in the "Class Builder
"The exit caused by the throw statement in" will cause the object pointed to by this pointer not to be refactored. It's very basic and it's not going to go deep.
As shown, you can change the pointer to a class, such as a template class instead of a pointer, and set a destructor inside the template class.
(ii), the statement "throw;" Throws an exception that cannot be caught, even if it is a catch (...) Can not be captured, then enter the termination function
, see catch below.
3. In-depth catch:
The general catch occurs in the form of:
try{}
catch (except1&) {}
catch (except2&) {}
catch (...) {}//Accept all exceptions
Generally written as references (except1&), the reason is simple, efficient.
Question A: What if the exception is thrown, but the catch is not an exception? (Note that there is no Java-like finally statement)
The default termination function is called when a catch does not catch a matching exception. You can call Set_terminate () to set the terminating function, which is a function pointer with the type: void (*terminate) ().
Here, can be a question: "No try-catch, directly in the program" throw; ”
Some other tricks:
4, try a function body, the form is as follows
void Fun (type1,type2) Try----try placed behind function body
{
function definition
}
catch (Typex) {}
The effect of this usage is equivalent to:
void Fun ()
{
try{function Definition}
}
5, throw a function body, the form is as follows:
void Fun (); Can throw any type of exception
void Fun () throw (EXCEPT1,EXCEPT2,EXCEPT3)
Inside the parentheses is an exception parameter table, this example can only throw in the 3 exception
void Fun () throw ()//Parameter table is empty, cannot throw exception
Question B: What happens if you throw an exception that is not in the "Exception parameter table" in Fun ()?
Answer: Call the terminating function set in Set_terminate (). However, this is only a superficial phenomenon, in effect invoking the default unexpected () function, but this default unexpected () calls the terminating function set in Set_terminate (). The unexpected can be set with set_unexpected (), just like set_terminate (), but after the new "unexpected ()" is set, the terminating function set in Set_terminater is no longer called.
This syntax is very useful, because in the use of other people's code, do not know which place will call what function and throw what exception, with an exception parameter table in the declaration of limitations, very practical.===============================
Reference
- http://laokaddk.blog.51cto.com/368606/214051
- Http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt02ch06s02.html
- Cppreference
C + + try throw catch