Chapter 4 tools for large programs (4)

Source: Internet
Author: User

17.1.7 exception levels

The unique operation defined by the exception type is a virtual member named what. This function returns the const char * object, which generally returns information used to construct the exception object at the throwing position. Because what is a virtual function, if the base class type reference is captured, the call to what function will execute a dynamic version suitable for the exception object.

1. Exceptions used for bookstore applications

Applications should also often extend the exception level by deriving additional types from the exception class or intermediate base class. These new Derived classes can represent exception types specific to the application field.

# Ifndef BOOKEXCEPTION_H
# Define BOOKEXCEPTION_H
 
# Include <iostream>
# Include <string>
Using namespace std;
 
Class out_of_stock: public runtime_error {
Public:
Explicit out_of_stock (const string & message): runtime_error (message ){}
};
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 (){}
};
# Endif
# Ifndef BOOKEXCEPTION_H
# Define BOOKEXCEPTION_H

# Include <iostream>
# Include <string>
Using namespace std;

Class out_of_stock: public runtime_error {
Public:
Explicit out_of_stock (const string & message): runtime_error (message ){}
};
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 (){}
};
# Endif2. use the exception type defined by the programmer

Try {
Throw out_of_stock ("Here is the exception of out_of_stock ...");
}
Catch (out_of_stock e ){
Cout <e. what () <endl;
}
Try {
Throw out_of_stock ("Here is the exception of out_of_stock ...");
}
Catch (out_of_stock e ){
Cout <e. what () <endl;
} 17.1.8 Automatic Resource release

The running of destructor leads to the emergence of an important programming technology, which makes the program more exceptional and secure (exception safe ). Abnormal security means that the program can operate normally even if an exception occurs. In this case, "security" comes from ensuring that "if an exception occurs, all resources allocated are properly released ".

By defining a class to encapsulate resource allocation and release, you can ensure that resources are correctly released. This technology is often called "resource allocation is initialization", or RAII for short.

Classes should be used to manage abnormal programs and resource allocation programs. As described in this section, you can use class management to allocate and Recycle resources to ensure that resources are released in case of exceptions.

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.