面向对象程序设计 C++异常处理.pptVIP

  • 4
  • 0
  • 约1.09千字
  • 约 15页
  • 2017-04-21 发布于浙江
  • 举报
异常处理;本章学习要求:;1 异常的概念;2 C++的异常处理机制 ;2.1 抛出异常throw ;2.2 捕获和处理异常try、catch ;;;;2.3 自定义异常类 ;例 编写函数divide实现两数相除,除数为0时抛出自定义异常。 #include iostream #include string using namespace std; class ZeroDivide{ public: ZeroDivide(string desc){ this-desc = desc; } string getMessage(){ return desc; } private: string desc; }; int divide(int x, int y){ if (y==0) throw ZeroDivide(除数为0); //发现并抛出异常 return x/y; } int main(){ int x, y; cout 输入两个数: ; while (cin x y){ try{ cout divide(x, y) endl; } catch(ZeroDivide e){ //捕获并处理异常 cout e.getMessage() endl; } cout 继续输入数值 q退

文档评论(0)

1亿VIP精品文档

相关文档