throw(),空异常描述符,中断描述符

在分析stl源码的时候,发现几乎所有函数都包含空异常描述符throw().

那么throw()究竟有何意义呢?

其实,与其称其为"空异常描述符",还不如称其为"中断描述符".简单的说,如果throw()修饰的函数中发生任何异常,即便函数外部有捕获操作,函数也不会抛出任何异常,那么程序将直接终止!!!简单的说,对使用throw()修饰的函数进行try{}catch(...){}是没有任何意义的!顾,在使用throw()修饰函数的时候,请务必确认,函数在任何情况下都不会发送异常,顾,其大量存在stl之类非常非常严谨的代码中!!!

/* 
 * File:   main.cpp
 * Author: Vicky.H
 *
 */
#include <iostream>

class myexception : public std::exception {
public:

    const char* what() const throw () {
        std::cout << "my exception" << std::endl;
    }
};

void f1(bool flag = true) {
    if (flag) throw myexception();
}

void f2(bool flag = true) throw () {
    if (flag) throw myexception();
}

/*
 * 
 */
int main(void) {

    try {
        f1();
    } catch (...) {
    }
    std::cout << "f1()抛出的异常将会被捕获,程序不会中断,将继续执行" << std::endl;
    
    try {
        f2();
    } catch (...) {
    }
    std::cout << "f2()抛出的异常不会被捕获,程序会中断,将不会执行该条语句" << std::endl;

    return 0;
}


 

f1()抛出的异常将会被捕获,程序不会中断,将继续执行
my exception

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'myexception'
  what():  L3蚾`3蚾

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值