#include
#include
jmp_buf static_buf; //用来存放处理器上下文,用于跳转
void do_jmp()
{
//do something,simetime occurs a little error
//调用longjmp后,会载入static_buf的处理器信息,然后第二个参数作为返回点的setjmp这个函数的返回值
longjmp(static_buf,10);//10是错误码,根据这个错误码来进行相应的处理
}
int main()
{
int ret = 0;
//将处理器信息保存到static_buf中,并返回0,相当于在这里做了一个标记,后面可以跳转过来
if((ret = setjmp(static_buf)) == 0) {
//要执行的代码
do_jmp();
} else { //出现了错误
if (ret == 10)
std::cout << "a little error" << std::endl;
}
}
#include
#include
using namespace std;
class base {
public:
base() {
cout << "base construct func call" << endl;
}
~base() {
cout << "~base destruct func call" << endl;
}
};
jmp_buf static_buf;
void test_base() {
base b;
//do something
longjmp(static_buf,47);//进行了跳转,跳转后会发现b无法析构了
}
int main() {
if(setjmp(static_buf) == 0) {
cout << "deal with some thing" << endl;
test_base();
} else {
cout << "catch a error" << endl;
}
}
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0):data(msg)
{
//idle
}
};
void do_error() {
throw MyError("something bad happend");
}
int main()
{
do_error();
}
try{
//do something
throw string("this is exception");
} catch(const string& e) {
cout << "catch a exception " << e << endl;
}
#include
using namespace std;
int main()
{
try{
throw 'a';
}catch(int a) {
cout << "int" << endl;
}catch(char c) {
cout << "char" << endl;
}
}
//基类
class Base{
public:
Base(string msg):m_msg(msg)
{
}
virtual void what(){
cout << m_msg << endl;
}
void test()
{
cout << "I am a CBase" << endl;
}
protected:
string m_msg;
};
//派生类,重新实现了虚函数
class CBase : public Base
{
public:
CBase(string msg):Base(msg)
{
}
void what()
{
cout << "CBase:" << m_msg << endl;
}
};
int main()
{
try {
//do some thing
//抛出派生类对象
throw CBase("I am a CBase exception");
}catch(Base& e) { //使用基类可以接收
e.what();
}
}
try {
//do some thing
throw CBase("I am a CBase exception");
}catch(Base e) {
e.test();
}
try{
throw Exception("I am a exception");
}catch(...) {
//log the exception
throw;
}
#include
#include
#include
using namespace std;
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0):data(msg)
{
//idle
}
};
void do_error() {
throw MyError("something bad happend");
}
//自定义的terminate函数,函数原型需要一致
void terminator()
{
cout << "I'll be back" << endl;
exit(0);
}
int main()
{
//设置自定义的terminate,返回的是原有的terminate函数指针
void (*old_terminate)() = set_terminate(terminator);
do_error();
}
#include
#include
using namespace std;
class base
{
public:
base()
{
cout << "I start to construct" << endl;
if (count == 3) //构造第四个的时候抛出异常
throw string("I am a error");
count++;
}
~base()
{
cout << "I will destruct " << endl;
}
private:
static int count;
};
int base::count = 0;
int main()
{
try{
base test[5];
} catch(...){
cout << "catch some error" << endl;
}
}
I start to construct I start to construct I start to construct I start to construct I will destruct I will destruct I will destruct catch some error
#include
using namespace std;
int main() try {
throw "main";
} catch(const char* msg) {
cout << msg << endl;
return 1;
}
class Base
{
public:
Base(int data,string str)try:m_int(data),m_string(str)//对初始化列表中可能会出现的异常也会进行捕捉
{
// some initialize opt
}catch(const char* msg) {
cout << "catch a exception" << msg << endl;
}
private:
int m_int;
string m_string;
};
int main()
{
Base base(1,"zhangyifei");
}
#include
#include
#include
using namespace std;
class MyError:public runtime_error {
public:
MyError(const string& msg = "") : runtime_error(msg) {}
};
//runtime_error logic_error 两个都是继承自标准异常,带有string构造函数
//
int main()
{
try {
throw MyError("my message");
} catch(MyError& x) {
cout << x.what() << endl;
}
}
#include
#include
#include
#include
using namespace std;
class Up{};
class Fit{};
void g();
//异常规格说明,f函数只能抛出Up 和Fit类型的异常
void f(int i)throw(Up,Fit) {
switch(i) {
case 1: throw Up();
case 2: throw Fit();
}
g();
}
void g() {throw 47;}
void my_ternminate() {
cout << "I am a ternminate" << endl;
exit(0);
}
void my_unexpected() {
cout << "unexpected exception thrown" << endl;
// throw Up();
throw 8;
//如果在unexpected中继续抛出异常,抛出的是规格说明中的 则会被捕捉程序继续执行
//如果抛出的异常不在异常规格说明中分两种情况
//1.异常规格说明中有bad_exception ,那么会导致抛出一个bad_exception
//2.异常规格说明中没有bad_exception 那么会导致程序调用ternminate函数
// exit(0);
}
int main() {
set_terminate(my_ternminate);
set_unexpected(my_unexpected);
for(int i = 1;i <=3;i++)
{
//当抛出的异常,并不是异常规格说明中的异常时
//会导致最终调用系统的unexpected函数,通过set_unexpected可以
//用来设置自己的unexpected汗函数
try {
f(i);
}catch(Up) {
cout << "Up caught" << endl;
}catch(Fit) {
cout << "Fit caught" << endl;
}catch(bad_exception) {
cout << "bad exception" << endl;
}
}
}
}
void recoup(int) noexecpt(true); //recoup不会抛出异常 void recoup(int) noexecpt(false); //recoup可能会抛出异常
template<typename T> T stack<T>::pop()
{
if(count == 0)
throw logic_error("stack underflow");
else
return data[--count];
}
class Bitmap {...};
class Widget {
...
private:
Bitmap *pb;
};
Widget& Widget::operator=(const Widget& rhs)
{
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
Widget& Widget::operator=(const Widget& rhs)
{
If(this == rhs) return *this; //证同性测试
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
Widget& Widget::operator=(const Widget& rhs)
{
If(this == rhs) return *this; //证同性测试
Bitmap *pOrig = pb;
pb = new Bitmap(*rhs.pb); //现在这里即使发生了异常,也不会影响this指向的对象
delete pOrig;
return *this;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有