C++边边角角(二)

#include <stdio.h>

struct Ex
{
static int gId;
int id;
Ex(){id=gId++;printf("Ex()-id=%d/n",id);}
Ex(const Ex &e){id=gId++;printf("Ex(e)-id=%d,copy from %d/n", id, e.id);}
~Ex(){printf("~Ex()-id=%d/n",id);}
};
int Ex::gId;

void func1(void)
{
try
{
throw Ex(); // create
}
catch (Ex e) // copy
{
} //all destructed
}
void func2(void)
{
try
{
throw Ex(); // create
}
catch (Ex &e)
{
} //all destructed
}
void func3(void)
{
try
{
Ex e; // create
throw e; // copy, why it needs to be copied, see func6
}
catch (Ex e) // copy
{
} //all destructed
}
void func4(void)
{
try
{
Ex e; // create
throw e; // copy
}
catch (Ex &e)
{
} //all destructed
}
void func5(void)
{
Ex e; // create (1)

try
{
throw e; // copy (2)
}
catch (Ex &e)
{
printf("leaving5-exh/n");
} //(2)destructed
printf("leaving5/n"); // (1) destructed
}
void func6(void)
{
Ex e; // create (1)
try
{
throw e; // copy (2), this one is copied so that it can be delivered to the caller
// i guess there is some mem area as oppposed to the calling stack used
// to hold the exception object. So once throwing is invoked, an exception object
// is always created either by ordinary constructor or copy constructor
.
}
catch(int)
{
}
//(1) destructed
}


int main(void)
{
printf("========func1========/n");
func1();
printf("========func2========/n");
func2();
printf("========func3========/n");
func3();
printf("========func4========/n");
func4();
printf("========func5========/n");
func5();
printf("========func6========/n");
try{
func6();
}
catch(...)
{
printf("dealing with ex from func6/n");
} //(2) destructed
printf("finished/n");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值