C++异常处理机制

1、默认异常处理机制about()

默认状况下,当程序出现异常,将调用about()异常处理函数,直接终止程序。缓存

2、exit()

exit()将刷新缓存区,但不显示消息。dom

3、try-catch-throw

try{}:当代码块内出现异常,并不会终止程序,而是将程序跳转到catch代码块,
catch(){}:正常状况下,catch代码块不会执行,只有当程序异常时才会执行,
throw(){}:此时若是异常程序内使用了throw来处理,则throw会将信息做为参数传递给catch,catch处理信息后继续执行程序。
一般状况下,catch经过传递类来处理异常。函数

//.h
class bad
{
    private:
        double v1;
        double v2;
    public:
        bad(int a=0,int b=0):v1(0),v2(0){}
        void msg(){std::cout<<"hmean("<<v1<<","<<v2<<"):"<<"invaliud argument:a=-b;\n"}
};

//.c

int hmean(int a,int b);

int main()
{

    try
    {
        c=hmean(a,b);
    }
    catch(bad& bg)
    {
        bg.msg();
        std::cout<<"Error!"
        continue;
    }
    std::cout<<"Bye!";
    return 0;
}

int hnean(int a,int b)
{
    return a/b;
}

4、exception类

exception类包含在<exception.h>头文件中,
exception类是一种异常类的基类,派生出两种类:logic_error和runtime_error,
logic_error和runtime_error
logic_error描述了典型的逻辑错误(可修复),runtime_error描述了运行时错误(不可避免)。
logic_error
· domain_error,定义域错误,参数超出取值范围
· invalid_error,无效参数,参数类型错误
· length_error,没有足够的空间执行操做
· out_of_bounds,索引错误
runtime_error
· range_error,值域错误,计算结果不在不在函数容许范围内,但没有发生上溢错误和下溢错误
· overflow_error,上溢错误,计算结果超出类型能表示的最大值
· underflow_error,下溢错误,发生在浮点运算中,通常浮点运算存在最小值,超出这个最小值将发生下溢错误
bad_alloc
当使用new致使内存分配问题时,将引起bad_alloc异常,
bad_alloc位于头文件 中, ui

try
{
    int* ptr=new int[100000000];
}
catch(bad_alloc& ba)
{
    std::cout<<ba.what()<<std::endl;
    exit(EXIT_FALLURE);
}

5、异常嵌套类

将异常类做为嵌套类放在要原类中能够精简代码,加强可读性。指针

6、未捕获异常

当发生异常try-catch未能捕获异常时,程序不会当即终止,而是发生如下行为:
调用unexceptd(),
调用terminate(),
调用about().code

重定向terminate()索引

#include<exception>

void myQuit()
{
    std::cout<<"myError!"<<std::endl;
    exit(5);
}

//而后在程序开头使用set_terminate()重定向terminate()
set_terminate(myQuit);

类似的unexceptd()也可重定向,可是只能重定向为terminate(),about()和exit()来终止程序内存

#include<exception>

void myUnexcepted()
{
    //为了捕获全部异常,能够定向为bad_exception
    throw std::bad_exception();
}

//而后在程序开头使用set_terminate()重定向terminate()
set_unexcepted(myUnexceptd);

7、注意事项

为防止使用new形成内存泄漏,建议使用智能指针。it

相关文章
相关标签/搜索