Article Directory
8. C++ Standard Exception Class
The root class in the inheritance hierarchy of the C++ standard library exception class is exception, which is defined in the exception header file. It is the base class for all functions of the C++ standard library to throw exceptions. The interface of exception is defined as follows:
namespace std {
class exception {
public:
exception() throw(); //不抛出任何异常
exception(const exception& e) throw();
exception& operator= (const exception& e) throw();
virtual ~exception() throw)();
virtual const char* what() const throw(); //返回异常的描述信息
};
}
Let's first look at the direct derived class of the exception class:
Exception name | Description |
---|---|
logic_error | logical error. |
runtime_error | Runtime error. |
bad_alloc | Exception thrown when memory allocation fails using new or new[ ]. |
bad_typeid | Use typeid to manipulate a NULL pointer, and the pointer is a class with virtual functions, then a bad_typeid exception is thrown. |
bad_cast | The exception thrown when the dynamic_cast conversion fails. |
ios_base::failure | An exception occurred during io. |
bad_exception | This is a special exception. If a bad_exception exception is declared in the exception list of the function, when an exception not in the exception list is thrown inside the function, if an exception is thrown in the unexpected() function called, no matter what type it is, it will be Replace with bad_exception type. |
Derived class of logic_error:
Exception name | Description |
---|---|
length_error | This exception is thrown when trying to generate an object that exceeds the maximum length of the type, such as the resize operation of a vector. |
domain_error | The value range of the parameter is wrong, which is mainly used in mathematical functions, such as using a negative value to call a function that can only operate on non-negative numbers. |
out_of_range | Out of the valid range. |
invalid_argument | The parameters are inappropriate. In the standard library, when the string object is used to construct a bitset, and the character in the string is not 0 or 1, this exception is thrown. |
Derived class of runtime_error:
Exception name | Description |
---|---|
range_error | The calculation result exceeds the meaningful value range. |
overflow_error | Arithmetic calculation overflowed. |
underflow_error | Arithmetic calculation underflows. |
Nine, write your own exception class
Principle: It is recommended to inherit the standard exception class and overload the what function and destructor of the parent class
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<stdexcept>
using namespace std;
class Person {
public:
Person() {
mAge = 0;
}
void setAge(int age) {
if (age < 0 || age > 100) {
throw out_of_range("年龄应该在0-100之间!");
}
this->mAge = age;
}
public:
int mAge;
};
//test01()使用标准库的异常类,下面的exception可以换为out_of_range
void test01() {
Person p;
try {
p.setAge(1000);
}
catch (exception e) {
cout << e.what() << endl;
}
}
//自己写个异常类,重载父类的what函数和析构函数
class MyOutOfRange : public exception {
public:
MyOutOfRange(const char* error) {
pError = new char[strlen(error) + 1];
strcpy(pError, error);
}
~MyOutOfRange() {
if (pError != NULL) {
delete[] pError;
}
}
virtual const char* what() const {
return pError;
};
public:
char* pError;
};
void fun02() {
throw MyOutOfRange("我自己的out_of_range!");
}
void test02() {
try {
fun02();
}
catch (exception& e) {
cout << e.what() << endl;
}
}
int main(void)
{
test01();//结果:年龄应该在0-100之间!
//test02();//结果:我自己的out_of_range!
return 0;
}
10. The application of inheritance in exceptions
Try to throw a class object (base class) for exceptions instead of -1 or char*.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
//异常基类
class BaseMyException {
public:
virtual void what() = 0;
virtual ~BaseMyException() {
}
};
class TargetSpaceNullException : public BaseMyException {
public:
virtual void what() {
cout << "目标空间空!" << endl;
}
~TargetSpaceNullException() {
}
};
class SourceSpaceNullException : public BaseMyException {
public:
virtual void what() {
cout << "源空间为空!" << endl;
}
~SourceSpaceNullException() {
}
};
void copy_str(char* taget, char* source) {
if (taget == NULL) {
throw TargetSpaceNullException();
}
if (source == NULL) {
throw SourceSpaceNullException();
}
//int len = strlen(source) + 1;
while (*source != '\0') {
*taget = *source;
taget++;
source++;
}
}
int main(void) {
const char* source = "abcdefg";
char buf[1024] = {
0 };
try {
copy_str(buf, NULL);
}
catch (BaseMyException& ex) {
ex.what();
}
cout << buf << endl;
return 0;
}
//结果:源空间为空!