C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下:
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(); //返回异常的描述信息 };}
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(); //返回异常的描述信息
};
}
先来看一下 exception 类的直接派生类:
logic_error 的派生类:
runtime_error 的派生类:
原则:建议继承标准异常类,并重载父类的what函数和析构函数
#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_rangevoid 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;}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<stdexcept>
using namespace std;
class Person {
Person() {
mAge = 0;
void setAge(int age) {
if (age < 0 || age > 100) {
throw out_of_range("年龄应该在0-100之间!");
this->mAge = age;
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 {
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;
char* pError;
void fun02() {
throw MyOutOfRange("我自己的out_of_range!");
void test02() {
fun02();
catch (exception& e) {
int main(void)
{
test01();//结果:年龄应该在0-100之间!
//test02();//结果:我自己的out_of_range!
return 0;
异常尽量抛个类对象(基类),不要再用 -1 或 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;}//结果:源空间为空!
//异常基类
class BaseMyException {
virtual void what() = 0;
virtual ~BaseMyException() {}
class TargetSpaceNullException : public BaseMyException {
virtual void what() {
cout << "目标空间空!" << endl;
~TargetSpaceNullException() {}
class SourceSpaceNullException : public BaseMyException {
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 };
copy_str(buf, NULL);
catch (BaseMyException& ex) {
ex.what();
cout << buf << endl;
//结果:源空间为空!
原文链接:http://www.cnblogs.com/gqw-myblogs/p/14286832.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728