随笔 - 181  文章 - 15  trackbacks - 0
<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(1)

随笔分类

随笔档案

My Tech blog

搜索

  •  

最新评论

阅读排行榜

评论排行榜

;C++标准库风格迥异.它包含了设计和实现风格各不相同的不同来源的软件.错误处理和异常处理是一个典型.标准库中的string类们支持更加细化的错误处理.它们会监测每一处有可能发生问题的地方并会在有错误的时候抛出异常.另外一些,比如STL和valarrays比较安全性而言更加注重效率.所以它们很少监测逻辑错误,并仅仅在运行时错误发生的时候才抛出异常.
有三种标准异常类:
1.语言支持型异常(Exceptions for language support )
比如:
用new创建对象失败的时候,会抛出bad_alloc异常;
dynamic_cast失败会抛出bad_cast异常;
函数抛出不可控异常的时候会抛出bad_exception异常或调用unexpected函数.

#include <iostream>
class E1
{

};
class E2
{
};
void f_throwE1() throw(E1)
{
    
throw E1();
}
void f_throwE2() throw(E1)
{
    
throw E2();
}
void f_throwE2AsBadException() throw (E1,std::bad_exception)
{
    
throw E2();
}
int main()
{
    
try
    {

        f_throwE1(); 
//1:抛出可控异常
        f_throwE2(); //2:抛出不可控异常,执行unexpected函数
        f_throwE2AsBadException(); //3:抛出不可控异常,被认为是bad_exception
    }
    
catch(const E1 &e1)
    {
        std::cout
<<"E1 error occured"<<std::endl;    
    }
    
catch(const std::bad_exception &e2)
    {
        std::cout
<<"bad_exception occured"<<std::endl;
    }
    
catch(const E2 &e2)
    {
        std::cout<<"E2 error occured"<<std::endl;
    }
    catch(...)
    {
        std::cout<<"unknown exception occured"<<std::endl;
    }
}
上面的程序中,执行1,会产生如下的结果:
E1 error occured
执行2,并没有被作为类型E2的异常所捕获.而是出现了如下提示:
terminate called after throwing an instance of 'E2'
忽略
现在执行3.结果并没有捕获到std::bad_exception异常.而是仍然出现了这样的错误提示:
terminate called after throwing an instance of 'E2'
忽略
同样的的例子,上面是在linux下执行的情况.下面换做vs2003的vc7.执行3,结果是:
unknown exception occured
即也没有捕获到std::bad_exception,但是被更加通用的...所捕获了.这是编译器情况的不同.
2.c++标准库的异常(Exceptions for the C++ standard library)
c++标准库的异常类通常从logic_error类继承.逻辑错误从理论上来说,至少应该被程序所避免,例如通过对于函数参数进行额外的测试.c++提供了诸如invalid_argument,length error,out_of_range,domain_error等异常类.
3.在程序域之外的异常(Exceptions for errors outside the scope of a program)
此类异常通常难于避免.
异常类的头文件
异常的基类和bad_exception类被定义在<exception>里;
类bad_alloc在<new>中被声明;
bad_cast和bad_typeid在<typeinfo>中被声明;
ios_base::failure在<ios>中被声明;
所有其他的类都被定义在<stdexcept>中.
异常类的成员
在异常类中除类型信息之外,唯一可以获取的信息来自于函数what():
namespace std {
    class exception {
      public:
            virtual const char* what() const throw();
            ...
      };
}

使用what来获取信息:
try {
    ...
}
catch (const exception& error) {
    //print implementation-defined error message
    cerr << error.what() << endl;
    ...
}
可以以下面的方式创建标准异常:
string s;
...
throw out_of_range(s);

或者
throw out_of_range("out_of_range exception (somewhere, somehow)");
从标准异常类继承
例如:
#include <exception>
#include 
<iostream>
class MyException:public std::exception
{
private:
    
char* errorMessage;
public:
    MyException(
const char* errMsg)
    {
        errorMessage
=new char[strlen(errMsg)];
        strcpy(errorMessage,errMsg);
    }
    
~MyException() throw()
    {
        
if(errorMessage)
        {
            delete[] errorMessage;
            errorMessage
=NULL;
        }
    }
    
virtual const char* what() const throw()
    {
        
return errorMessage;
    }

};
int main()
{
    
try
    {
        
throw MyException("Error Occured!!");
    }
    
catch(const std::exception &e)
    {
        std::cout
<<e.what()<<std::endl;
    }

}






posted on 2007-06-26 22:18 littlegai 阅读(994) 评论(0)  编辑 收藏 引用 所属分类: 我的读书笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理