c++异常处理机制 (try—throw—catch的使用)
2015-03-02 17:56
337 查看
try ,catch 形式如下:
try { 可能出现错误的语句块; } catch (类型名[形参名]) { }
</pre><p></p><p>实例:</p><p></p><p><pre name="code" class="cpp">#include <iostream> #include <stdlib.h> using std::cout; using std::cin; using std::endl; double fuc(double a, double b)//定义函数; { if ( b == 0)// 如果除数为零,抛出异常; throw b; return a / b;//无异常就返回商; } int main(void) { try { double res; res = fuc(4, 2); cout << "res=" << res; cout <<endl; double resu = fuc(2,0); cout << resu; } catch (double)//捕捉异常; { cout<<"数据有误!"; exit(1); // } cout <<endl; system("pause"); return 0; }
相关文章推荐
- C++异常处理机制---try&throw&catch
- Try-Catch机制使用场景分析
- C#中使用try{}catch{}finally{}对系统性能的影响和处理机制(摘录)
- C++异常处理try throw catch
- 异常概念和处理机制,try-catch-finally,throw和throws,自定义异常
- 最新关于try ,catch,throw的使用小结
- Try-Catch机制使用场景分析
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- java:异常处理机制 try-catch-throw-finally
- 经验分享,如何使用try,catch, throw之二
- JavaScript_try...catch语句与throw结合使用示例
- Foreach语句,Throw语句,Try,catch语句的使用,报告编译错误
- try...catch...finally;throw与throws关键字联合使用
- C# 中异常抛出捕获机制--throw / try,catch,finally
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- C++异常接口声明和使用详解try catch throw
- Try-Catch机制使用场景分析
- C#中使用try{}catch{}finally{}对系统性能的影响和处理机制(摘录)
- java的异常机制五个关键字:throw try catch finally throw