Chapter 6 tools for large programs (6)

Source: Internet
Author: User

17.1.10 exception description

Exception specification specifies that if a function throws an exception, the thrown exception is included in the description or a type derived from the listed exception.

Auto_ptr Defects

(1) do not use the auto_ptr object to save the pointer to the static allocation object.

(2) Never use two auto_ptr objects to point to the same object.

(3) do not use the auto_ptr object to save the pointer pointing to the dynamically allocated array.

(4) do not store auto_ptr objects in containers.

1. Definition exception description

Exception description follows the function parameter table. An exception indicates the list of exception types enclosed in parentheses following the keyword throw (which may be null.

Void Method1 () throw (runtime_error ){
Try
{
Throw range_error ("error ~ ");
}
Catch (...)
{
Throw;
}
}
Void Method1 () throw (runtime_error ){
Try
{
Throw range_error ("error ~ ");
}
Catch (...)
{
Throw;
}
} The empty description list indicates that the function does not throw any exception.

Size_type size (const BaseManager & I) const throw (){
Return items. count (I );
}
Size_type size (const BaseManager & I) const throw (){
Return items. count (I );
}

Exception description is part of the function interface. The function definition and any declaration of the function must have the same exception description.

If no exception is specified in a function declaration, the function can throw any type of exception.

2. violation exception description

If a function throws an exception not listed in its exception description, it calls the standard library function unexpected. By default, the unexpected function calls the terminate function, which usually terminates the program.

During compilation, the compiler cannot and will not try to verify the exception description.

The compiler generates code to ensure that if an exception that violates the exception description is thrown, the unexpected function is called.

3. Make sure the function does not throw an exception.

An exception is useful when the function can ensure that no exception is thrown.

Knowing that a function does not throw an exception will simplify the compilation of exceptional and secure code that calls the function. We can know that you do not have to worry about exceptions when calling the function, and if the compiler knows that no exception will be thrown, it can execute the optimization that is restrained by code that may throw exceptions.

4. exception description and member functions

In the const member function declaration, the exception description follows the const qualifier.

Size_type size (const BaseManager & I) const throw (){
Return items. count (I );
}
Size_type size (const BaseManager & I) const throw (){
Return items. count (I );
} 5. exception description and destructor

Class isbn_mismatch: public logic_error {
Public:
Explicit isbn_mismatch (const string & message): logic_error (message ){}
Isbn_mismatch (const string & message, const string & lhs, const string & rhs)
: Logic_error (message), left (lhs), right (rhs ){}
Const string left, right;
Virtual ~ Isbn_mismatch () throw (){}
};
Class isbn_mismatch: public logic_error {
Public:
Explicit isbn_mismatch (const string & message): logic_error (message ){}
Isbn_mismatch (const string & message, const string & lhs, const string & rhs)
: Logic_error (message), left (lhs), right (rhs ){}
Const string left, right;
Virtual ~ Isbn_mismatch () throw (){}
}; 6. exception description and virtual functions

The exception description of the virtual function in the base class, which is different from the exception description of the corresponding virtual function in the derived class.

However, the exception description of the derived-class virtual function is as strict as that of the corresponding base-class virtual function, or is more restrictive than the latter.

This restriction ensures that when the pointer to the base class type is used to call the virtual function of the derived class, the exception description of the derived class does not add new exceptions that can be thrown.

The exception list in the base class is the superset of the exception list that can be thrown by the derived class version of the virtual function.

Class Book {
Public:
Virtual void Method1 () throw (logic_error ){}
};
 
Class NoteBook: public Book {
Public:
Void Method1 () throw (isbn_mismatch ){}
};
Class Book {
Public:
Virtual void Method1 () throw (logic_error ){}
};

Class NoteBook: public Book {
Public:
Void Method1 () throw (isbn_mismatch ){}
}; 17.1.11 function pointer exception description

Exception description is part of the function type. In this way, the exception description can also be provided in the definition of the function pointer.

When another pointer initializes a pointer to a function with an exception description, or assigns the latter a value to the function address, the exception description of the two pointers does not need to be the same, however, the description of the source pointer must be at least as strict as that of the target pointer.

Class Book {
Public:
Virtual void Method1 () throw (logic_error ){}
};
 
Class NoteBook: public Book {
Public:
Void Method1 () throw (isbn_mismatch ){}
};
Class Book {
Public:
Virtual void Method1 () throw (logic_error ){}
};

Class NoteBook: public Book {
Public:
Void Method1 () throw (isbn_mismatch ){}
}; Book book = Book ();
NoteBook notebook = NoteBook ();
Void (Book: * func_p) () throw (logic_error) = & (Book: Method1 );
(Book. * func_p )();
(Notebook. * func_p )();

From xufei96's column
 

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.