throw on catch
来源:互联网 发布:windows phone 知乎 编辑:程序博客网 时间:2024/06/05 05:29
序
见到cm中由于在catch中throw没catch引起的crash, 总结一下.
实验
// @file testcase05101520.cpp : Defines the entry point for the console application.// @brief test throw on catch// @note 对于描述为throw的c++的接口定义, 必须try-catch住.// 对于接口是否会抛出异常, 可以在接口描述中叙述, 也可以在接口定义中用throw描述// e.g. void CMyExp::DoThrowA(); void CMyExp::DoThrowB() throw(exception)#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#include <string>#include <exception>using namespace std;class CMyExp : public std::exception {public: CMyExp() {m_str_what = "MyExp";} virtual ~CMyExp() {} void setWhat(const char* pszWhat) {m_str_what = ((NULL != pszWhat) ? pszWhat : "MyExp");} const char* what () const throw () { return m_str_what.c_str(); } // @fn DoThrowA // @brief 接口中会抛出异常的例子A // @param NULL // @note 会抛出 exception e异常 // @return void void DoThrowA() { exception e("DoThrowA"); throw e; } // @fn DoThrowB // @brief 接口中会抛出异常的例子B // @param NULL // @return void void DoThrowB() throw(exception) { exception e("DoThrowA"); throw e; }private: std::string m_str_what;};void fnTryCatch1_doTask();void fnTryCatch2_tryAgain();int main(int argc, char* argv[]){ fnTryCatch1_doTask(); system("pause"); /** run result e.what = throw on fnTryCatch1_doTask e.what = throw on fnTryCatch2_tryAgain try again ok */ return 0;}void fnTryCatch1_doTask(){ CMyExp e; try { // do something, when error, throw e.setWhat("throw on fnTryCatch1_doTask"); throw(e); } catch (exception& e) { printf("e.what = %s\n", e.what()); // 可以在这里进行try-catch嵌套 // 也可以将try-catch写入另外的函数fnB, 整洁些, 然后在catch中调用那个函数fnB // 总之, 一个try(throw)必须要有一个catch, 否则throw或运行时其他异常(e.g. div0)没人接住,传到系统程序就报错了. // 这样, 在catch中throw也是可以正常运行的 fnTryCatch2_tryAgain(); }}void fnTryCatch2_tryAgain(){ CMyExp e; try { // do something, when error, throw // e.g. mongo c++ interface, maybe throw when call it // so when call a c++ interface declare indicate throw, we will try catch it e.setWhat("throw on fnTryCatch2_tryAgain"); throw(e); } catch (exception& e) { printf("e.what = %s\n", e.what()); printf("try again ok\n"); }}
0 0
- throw on catch
- throw catch
- try...catch...throw
- try catch throw用法
- catch,try,throw
- try catch throw用法
- try catch throw用法
- try,catch,throw
- try catch throw 使用
- try catch throw用法
- try catch throw用法
- try throw catch
- 抛出异常throw,catch
- try catch throw用法
- JavaScript try...catch throw
- C++ try/catch/throw
- try catch throw用法
- throw catch try
- WebService的开发手段
- 【算法】程序猿不写代码是不对的52
- 2017-05-04-Facebook出品的Android声明式开源新框架Litho文档翻译-总览和导航
- 数位DP-UESTC
- 一个图片压缩工具
- throw on catch
- STC单片机IO方波脉冲输出
- JS 图片转Base64编码
- LMAX架构
- 蓝桥杯之反幻方
- egret 必须先添加监听再设置skin
- BZOJ 4399: 魔法少女LJJ
- Android中的dex、apk、ClassLoader详解
- multiselect选项框