try-catch 处理异常,也即 C++ 异常处理

// 注意,这是 C++ 程序,文件名为: SEH-test.cpp
#include "stdio.h"
class A
{
public:
	void f1() {}
	// 抛出 C++ 异常
	void f2() { throw 888;}
};

// 这个函数中使用了 try-catch 处理异常,也即 C++ 异常处理
void test1()
{
A a1;
A a2,a3;
	try
	{
	a2.f1();
	a3.f2();
	}
	catch(int errorcode)
	{
	printf("catch exception,error code:%d\n", errorcode);
	}
}

// 这个函数没什么改变,仍然采用 try-except 异常机制,也即 SEH 机制
void test()
{
int* p = 0x00000000; // pointer to NULL
	__try
	{
	// 这里调用 test1 函数
	test1();
	puts("in try");
		__try
		{
		puts("in try");
		// causes an access violation exception;
		// 导致一个存储异常
		*p = 13;
		puts(" 这里不会被执行到 ");
		}
		__finally
		{
		puts("in finally");
		}
	puts(" 这里也不会被执行到 ");
	}
	__except(puts("in filter 1"), 0)
	{
	puts("in except 1");
	}
}

void main()
{
puts("hello");
	__try
	{
	test();
	}
	__except(puts("in filter 2"), 1)
	{
	puts("in except 2");
	}
puts("world");
}
/*
hello
catch exception,error code:888
in try
in try
in filter 1
in filter 2
in finally
in except 2
world
Press any key to continue
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值