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