C + + try throw catch

Source: Internet
Author: User
Tags throw exception

C + + try throw catch

These three are used together,

  1. Try {
  2. Statement list;
  3. }
  4. Catch(TypeA arg ) {
  5. Statement list;
  6. }
  7. Catch(TypeB arg ) {
  8. Statement list;
  9. }
  10. . . .
  11. Catch(Typen arg ) {
  12. Statement list;
  13. }
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
  1. #include <exception>
  2. #include <stdexcept>
  3. #include <iostream>
  4. using namespace Std;
  5. struct Argument_error : public std:: Runtime_error
  6. {
  7. Argument_error(const STD::string& S): Std:: Runtime_error(S) { }
  8. };
  9. int main(int argc)
  10. {
  11. STD:: set_terminate(__gnu_cxx:: __verbose_terminate_handler);
  12. Try{
  13. if (argc > 5)
  14. Throw Argument_error("ARGC is greater than 5!" );
  15. Else
  16. Throw argc;
  17. }
  18. Catch (Argument_error e){
  19. cout << "Argc:others"<< Endl;
  20. }
  21. Catch (int e){
  22. cout << "ARGC:" << E << Endl;
  23. }
  24. }
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
    1. http://laokaddk.blog.51cto.com/368606/214051
    2. Http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt02ch06s02.html
    3. Cppreference

C + + try throw catch

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.