七、异常
一个错误处理系统包含异常、标准错误码、错误日志记录以及监测系统
异常:
一种容错机制
基本思想:
让一个函数在发现了自己无法处理的错误时抛出(throw)一个异常,然后它的(直接或者间接)调用者能够处理这个问题。
异常处理机制是一种比较有效的处理系统运行时错误的方法。
C++针对异常处理提供了一种标准的方法,用于处理软件程序运行时的错误,并用于处理软件系统中可预知或不可预知的问题。
这样就可以保证软件系统运行的稳定性与健壮性
C++的异常处理机制:
由3部分组成:
try(检查)→throw(抛出)->catch(捕获)
异常处理的一般格式:
try{
if(错误){
throw异常
}
} catch(异常类型1){
进行异常处理的语句1
}
...
1.标准异常
abnormal.cpp
#include <iostream>
#include <string.h>
#include <stdexcept>
using namespace std;
int func(int, int) throw(runtime_error);
int func(int x, int y) throw(runtime_error){
if(y)
return x/y;
else{
runtime_error tmp("error, y=0");
throw tmp;
}
}
int main()
{
int a, b;
CIN:
cin >> a >> b;
try{
cout << func(a, b) << endl;
}catch(const runtime_error &err){
cout << err.what() << endl;
if( strcmp(err.what(), "error, y=0") == 0 )
goto CIN;
}
return 0;
}
abnormal.cpp
#include <iostream>
#include <string.h>
#include <stdexcept>
using namespace std;
int func(int, int) noexcept;
int func(int x, int y) noexcept{
if(y)
return x/y;
else{
runtime_error tmp("error, y=0");
throw tmp;
}
}
int main()
{
int a, b;
CIN:
cin >> a >> b;
try{
cout << func(a, b) << endl;
}catch(const runtime_error &err){
cout << err.what() << endl;
if( strcmp(err.what(), "error, y=0") == 0 )
goto CIN;
}
return 0;
}
2.自定义异常
MyException.cpp
#include <iostream>
#include <string.h>
#include <stdexcept>
using namespace std;
class MyException : public exception{
public:
MyException(const char *) noexcept;
virtual ~MyException() noexcept;
const char* what() const noexcept;
private:
const char *msg;
};
MyException::MyException(const char *errmsg) noexcept :msg(errmsg){ }
MyException::~MyException() noexcept{ }
const char* MyException::what() const noexcept{
return msg;
}
int func(int x, int y) throw(MyException){
if(y)
return x/y;
else{
throw MyException("error, y=0");
}
}
int main()
{
int a, b;
CIN:
cin >> a >> b;
try{
cout << func(a, b) << endl;
}catch(const MyException &err){
cout << err.what() << endl;
if( strcmp(err.what(), "error, y=0") == 0 )
goto CIN;
}
return 0;
}
3.转换函数
ConversionFunctions.cpp
#include <iostream>
using namespace std;
class Base{
public:
Base(int);
int GetValue();
operator int() const;
private:
int x;
};
Base::Base(int x):x(x){ }
int Base::GetValue(){ return x; }
Base::operator int() const{
return x;
}
int main()
{
Base obj(5);
int a = 13;
obj = a;
cout << obj.GetValue() << endl;
obj = 1;
a = obj;
cout << a << endl;
cout << obj << endl;
return 0;
}
ConversionFunctions.cpp
#include <iostream>
using namespace std;
class Base{
public:
Base(int);
int GetValue();
private:
int x;
};
Base::Base(int x):x(x){ }
int Base::GetValue(){ return x; }
class Demo{
public:
Demo(int);
operator Base() const;
private:
int x;
};
Demo::Demo(int x):x(x){ }
Demo::operator Base() const{
return x;
}
int main()
{
Base obj(5);
Demo obj1(5);
obj = obj1;
cout << obj.GetValue() << endl;
return 0;
}
4.explicit关键字
explicit.cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class Demo{
public:
Demo(int);
int GetValue();
private:
int x;
};
Demo::Demo(int x):x(x){ }
int Demo::GetValue(){ return x; }
class Base{
public:
explicit Base(int);
int GetValue();
private:
int x;
};
Base::Base(int x):x(x){ }
int Base::GetValue(){ return x; }
int main()
{
Demo obj = 6;
cout << obj.GetValue() << endl;
Base obj(4);
obj = Base(5);
return 0;
}
5.标准转换函数
StdConversionFunctions.cpp
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
class Base{
public:
Base(int);
private:
int x;
};
Base::Base(int x):x(x){ }
class Subclass:public Base{
public:
Subclass(int, int);
private:
int x;
};
Subclass::Subclass(int x, int y):Base(y), x(x){ }
int main()
{
char buf[12] = "aello world";
int *p = reinterpret_cast<int *>(buf);
cout << *p << endl;
cout << char (*p) << endl;
int q = reinterpret_cast<int>(buf);
cout << q << endl;
const char *str = "hello world";
char *s = const_cast<char *>(str);
Base obj1(2);
Subclass obj2(3, 8);
obj1 = obj2;
Subclass *m = static_cast<Subclass *>(&obj1);
return 0;
}
6.智能指针
SharedPtr.cpp
#include <iostream>
#include <memory>
using namespace std;
#define pri() cout<<"Line: "<<__LINE__<<endl;
class Base{
public:
Base(){ pri(); }
~Base(){ pri(); }
void func(){ pri(); }
};
void test(){
cout << "--------------" << endl;
shared_ptr<Base>m = make_shared<Base>(Base());
cout << "--------------" << endl;
shared_ptr<Base>p = m;
p->func();
m->func();
}
int main()
{
test();
return 0;
}
C++自学笔记(目录)
|