64-C++中的异常处理(上)
来源:互联网 发布:java windows 路径 编辑:程序博客网 时间:2024/06/05 02:47
1、C++异常处理
2、
#include <iostream>#include <string>using namespace std;double divide(double a, double b){ const double delta = 0.000000000000001; double ret = 0; if( !((-delta < b) && (b < delta)) ) { ret = a / b; } else { throw 0; } return ret;}int main(int argc, char *argv[]){ try { double r = divide(1, 0); cout << "r = " << r << endl; } catch(...) { cout << "Divided by zero..." << endl; } return 0;}Divided by zero...
3、
4、
5、
6、
通过throw抛出的异常,有对应的catch来捕获,严格按要求来匹配,
异常只能被捕获一次,
catch(…)为捕获所有异常,必须放在所有catch的最后一个位置上。
当没有捕获异常时要向上一级传,直到被处理为止。
当在try中抛出多个异常时,只捕获最后一个异常。
#include <iostream>#include <string>using namespace std;void Demo1(){ try { throw '1'; throw 1;//只捕获最后一个异常 } catch(char c) { cout << "catch(char c)" << endl; } catch(short c) { cout << "catch(short c)" << endl; } catch(double c) { cout << "catch(double c)" << endl; } catch(int c) { cout<<"catch(int c)"<<endl; } catch(...)//只能放在最后一个catch位置 { cout << "catch(...)" << endl; }}void Demo2(){ throw string("D.T.Software"); //throw "D.T.Software"; 抛出 const char *类型}int main(int argc, char *argv[]){ Demo1(); try { Demo2(); } catch(char* s) { cout << "catch(char *s)" << endl; } catch(const char* cs) { cout << "catch(const char *cs)" << endl; } catch(string ss) { cout << "catch(string ss)" << endl; } return 0;}catch(int c)catch(string ss)
7、小结
阅读全文
0 0
- 64-C++中的异常处理(上)
- c#中的异常处理
- C#中的异常处理(一)
- C#中的异常处理(二 )
- C#中的异常处理(一)
- C#中的异常处理(二 )
- C#中的异常处理(简单)一
- objective-c中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- C语言中的异常处理
- biztalk中的发送端口产生异常及处理(上)
- Visual Basic .NET中的异常处理简介(上)
- C++中的异常处理(一)——C和C++中异常处理的区别
- TCP包结构简介
- 判断对象存活方法及回收方法
- 欢迎使用CSDN-markdown编辑器
- 【1701H1】【穆晨】【171101】连续第二十二天总结
- Vulkan规范:第二章(1)
- 64-C++中的异常处理(上)
- Python实现 PAT 1003 我要通过
- CTFweb题型
- 【NOIP2017TG模拟2017.11.1】总结
- .csv文件的读取与写入
- Vulkan规范:第二章(2)
- Linux 基础操作(十五)————at延时任务、cron定时任务、临时文件管理
- 笨办法学Python习题1 第一个程序
- 01背包 newoj 2965: 采药