Chapter 18th tools for large programs

Source: Internet
Author: User

18.1 Exception Handling
Try {    //  actions that cause a exception to be thrown}catch  (...) {    // work to partially handle the exception    Throw ;}

In C + +, a exception is raised by throwing. When throw, the statement behind the throw is no longer executed and transferred to the catch, which means:

    1. Functions along the call chain may exit early
    2. One o'clock execution of exception handling code, objects created along the call chain will be destroyed
    3. When a exception is throw, it is expanded along the function call chain, looking for matching catch, if not found, it will be terminate.
    4. The object is automatically destroyed during the expansion process
    5. Because the destructor is automatically executed, the external cannot catch the exception, his exception should be captured and processed inside the destructor, and if not processed, the program will terminate
    6. If the object being thrown is a local object, it will not exist because of automatic destruction during the unwind process, and the object pointed to after the pointer is thrown must be guaranteed to exist.
18.1.2 Catching exceptions

The exception declaration in a catch can be used as a list of functions if the use can be null.

If the exception object in the catch is non-referenced, a copy will occur

Exception handling for exceptions should be placed closest to the exception, and general exceptions will intercept exceptions

Type conversions are generally not allowed in catch exceptions (allow non-const to const, derived classes to base classes, arrays | functions to pointers)

In exception handling, if the exception cannot be handled completely, it can be thrown again.

Catch can handle all exceptions, you can use "..." as parameters

18.1.3 function Try statement block and constructor

To handle the exception thrown by the constructor's initial value, the constructor must be written as function try blocks, because when the initial value is constructed (when the data is initialized as follows), it is not entered, so it cannot be processed in the function, only the function try statement block can be used. (This exception cannot handle an exception when IL constructs)

Template <typenameT>Blobtry  :    data (std::make_shared<std::vector<T> >(IL)) {    /*  empty body*/}catch (const STD :: Bad_alloc &e) {    handle_out_of_memory (e);}
18.1.4 Exception Description Noexcept
    1. If a function is followed by a noexcept, it means that the function does not throw an exception (both declarations and definitions must appear). It is also possible to approve a specified noexcept in the declaration of a function pointer.
    2. If the function is declared as noexcept, but an exception does occur, it is terminal, and the stack is not expanded.

To determine whether a call is an exception you use the unary operator noexcept () and return a bool value to indicate whether an exception will be thrown. If Noexcept (true) is placed after a function, indicating that this function does not throw an exception, if Noexcept (false) is placed after a function, this function may throw an exception.

// if G () determines that no exception is thrown and returns True, then F () determines no exception void F () noexcept (Noexcept (g ()));

Exception descriptions and pointers, virtual functions, and copy Control

If the function pointer has a noexcept specifier, you cannot bind a function that does not have a noexcept declaration to the top

If the function pointer does not have a noexcept description, you can bind any function to the top.

If a virtual function commits noexcept, the inherited derived class must also be declared as Noexcept

For a synthesized copy control member, if it is able to determine that no exception is thrown, the synthesized is also noexcept

18.1.5 Exception class Hierarchy

exception

Bad_alloc

None

Logic_error

Domain_error

Invalid_argument

Out_of_range

Length_error

Runtime_error

Overflow_error

Underflow_error

Range_error

Bad_cast

None

Custom exception Classes

class  Public std::runtime_error {public:    explicit out_of_stock (const std:: string &s):        Std::runtime_error (s) {}};
18.2 Namespaces
//Define Namespacesnamespacemy_namespace{//declare the members of other spaces here    usingstd::cout; //namespace Aliases    namespaceLib =std; voidFoo () {//Endl No declaration, must write completecout <<"My Namespace"<<Lib::endl; }    //nested namespaces, and are inline//members in an inline namespace do not have to be fully written when using theInlinenamespacemy_namespace_extends {}}classclassname{};//template exceptions must be in the original namespacenamespacestd{Template<>structHash<classname>;}//Unnamed namespaces//where the variable has a static declaration period, created before the first use, the program ends the destruction//can be discontinuous in a file and cannot span filesnamespace{}
18.2.3 classes, namespaces, and scopes

When a function is called, the namespace of the argument is automatically introduced.

18.3 multiple inheritance vs. virtual inheritance
  1. In a given derived list, the same base class can appear only once.
  2. A derived class constructor can initialize its immediate base class.
  3. The derived class inherits the constructor of the base class, but if there are multiple base classes with the same constructor parameters, you must customize the constructor, or you will get an error.
  4. If a function overload is used as a parameter for each base class, the type conversion will have a two semantic error when the function makes arguments to the object of the derived class
  5. Although a base class can occur one time in a derived list, you can also inherit the same class indirectly multiple times. By default, classes that inherit multiple times will correspond to separate parts that exist in different locations of the inheritance chain.
  6. If you want the class that inherits multiple times to correspond to only one part, use virtual inheritance.
  7. However, it is important to note that when a derived class inherits, virtual inheritance is implemented only when the relationship between the same indirect base class that corresponds to its base class is fictitious.
     class  raccoon: public  virtual  zooanimal { ...  */  class  Bear: virtual  /*   ...  */  class  Panda:raccoon, bear {/*   */}; 
  8. The panda of a derived class needs to control its virtual base class section on its own, and if Panda has no control, its default constructor is used, and the virtual base class is first constructed.
    class Panda:raccoon, bear{    Panda (std::stringbool  onexhibit)        "  Panda"), Bear        (name, onexhibit),        Raccoon (name, onexhibit),        Endangered ( endangered::critical),        Sleeping_flag (false) {}};
  9. Destructors are the inverse of the order in which the constructors are called.

Chapter 18th tools for large programs

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.