admin 管理员组

文章数量: 1087135


2024年3月19日发(作者:linux自动化部署脚本)

try ... catch 与 __try ... __except

2009年09月24日

VC中的这两个东西肯定谁都用过, 不过它们之间有什么区别, 正好有时间研究了一下,

如果有错误欢迎拍砖.

基于VC2005, 32位XP 平台测试通过. 估计对于其他版本的VC和操作系统是不通用

的.

1. try ... catch

这个是C++语言定义的, 每个C++都有对其的不同的实现. 使用也很简单. 比如我们

有一个函数, 读入年龄. 如果<=0 或者 >=100, 抛出异常:

int readAge() {

int age = 读入年龄;

if (age <=0 || age >= 100) {

throw AgeException(age);

}

return age;

}

其中 AgeException 的定义为

class AgeException {

public:

int errorAge;

AgeException(int age) {

errorAge = age;

}

};

在使用的时候也比较简单,

try {

int i = readAge();

printf("Age inputed is %d", i);


本文标签: 部署 估计 使用 读入 年龄