#include <stdio.h>
class MyException
{
public:
MyException() {printf(" 构造一个 MyException 对象 \n");}
MyException(const MyException& e) {printf(" 复制一个 MyException 对象 \n");}
operator=(const MyException& e) {printf(" 复制一个 MyException 对象 \n");}
~MyException() {printf(" 析构一个 MyException 对象 \n");}
};
class A
{
public:
A() {printf(" 构造一个 A 对象 \n");}
~A() {printf(" 析构一个 A 对象 \n");}
void f1() {}
// 抛出 C++ 异常
void f2() {MyException e; throw e;}
};
void test()
{
int* p = 0x00000000; // pointer to NULL
__try
{
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 test1()
{
A a1;
A a2,a3;
try
{
// 这里会产生一个 SEH 类型的系统异常
test();
a2.f1();
a3.f2();
}
// 捕获得到吗?
catch(...)
{
printf("catch unknown exception\n");
}
}
void main()
{
puts("hello");
__try
{
test1();
}
__except(puts("in filter 2"), 0)
{
puts("in except 2");
}
puts("world");
}
/*
hello
构造一个 A 对象
构造一个 A 对象
构造一个 A 对象
in try
in try
in filter 1
in finally
catch unknown exception
析构一个 A 对象
析构一个 A 对象
析构一个 A 对象
world
Press any key to continue
*/
// 捕获得到吗? catch(...)
最新推荐文章于 2025-08-12 21:47:50 发布