例:处理除零异常 iincludeiostream> int Div(int x, int y) int main( [cout<<5/2=<<Div (5, 2 <<end cout"8/0="≤<Dv(820≤≤end; cout"71="≤Dv(7,1)≤ Kend; catch(int) i cout<< except of deviding zero. In",1 cout<< that is ok,n 程序运行结果如下 int Div(int x, int y) 5/2=2 fiy=howy;抛出的是 except of deviding zero. return xly; 整数类型 that is ok 11
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 11 例:处理除零异常 #include<iostream> int Div(int x,int y); int main() { try { cout<<"5/2="<<Div(5,2)<<endl; cout<<"8/0="<<Div(8,0)<<endl; cout<<"7/1="<<Div(7,1)<<endl; } catch(int) { cout<<"except of deviding zero.\n"; } cout<<"that is ok.\n"; } int Div(int x,int y) { if(y==0) throw y; return x/y; } 程序运行结果如下: 5/2=2 except of deviding zero. that is ok. 抛出的是 整数类型
16.3 Example: Handling an Attempt to Divide e by zero ●抛出的异常可以是任何类型的对象,如枚举、整 数等等。但最常用的是类对象 ●异常类——都派生自 exception类(最高级) 0 2018, SEU. All rights reserved. 12
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 12 16.3 Example: Handling an Attempt to Divide by Zero ⚫ 抛出的异常可以是任何类型的对象,如枚举、整 数等等。但最常用的是类对象。 ⚫ 异常类——都派生自exception类(最高级)
16.3 Example: Handling an Attempt to Divide by Zero(抛出异常类对象的模式) #include <stdexcept> using std runtime error, Class DivideByzeroException public runtime error ipublic: DivideByzeroExceptiono runtime_error("attempted to divide by zero")0 } 程序 解 16.1~2 0 2018, SEU. All rights reserved. 13
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 13 16.3 Example: Handling an Attempt to Divide by Zero(抛出异常类对象的模式) #include <stdexcept> using std::runtime_error; Class DivideByZeroException : public runtime_error {public: DivideByZeroException() : runtime_error( "attempted to divide by zero" ) {} };
异常类层次结构 C ception Inv ou en runtime error logic error 对象 do overflow_error underflow_ error invalid_argument length_error out_of_range ar bad_alloc bad_cast bad_ type_id bad_exception ovI underflow error异常,报告算术下溢错误。 以上三个异常是由 runtime errol类派生的。 bad_aloc异常。当new()操作符不能分配所要求的存储区时,会抛 出该异常。它是由基类 exception派生的。 0 2018, SEU. All rights reserved. 14
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 14 异常类层次结构 C++标准库提供的逻辑异常: invalid_argument异常,接收到一个无效的实参,抛出该异常。 out_of_range异常,收到一个不在预期范围中的实参,则抛出。 length_error异常,报告企图产生“长度值超出最大允许值”的对象 domain_error异常,用以报告域错误(domain error)。 C++标准库提供的运行时异常() range_error异常,报告内部计算中的范围错误。 overflow_error异常,报告算术溢出错误。 underflow_error异常,报告算术下溢错误。 以上三个异常是由runtime_error类派生的。 bad_alloc异常。当new()操作符不能分配所要求的存储区时,会抛 出该异常。它是由基类exception派生的
<exception> exception类的接口如下: namespace stdt/注意在名字空间域std中 class exception public exception( throw;/默认构造函数 exception( const exception&) throw0;∥)制构造函数 exception &operator=(const exception&) throw(; ∥/复制赋值操作符 virtual~ exception0 throw;/析构函数 virtual const char* what const throw /返回一个C风格的字符串,目的是为抛出的异常提供文本 描述 O 2018, SEU. All rights reserved. 15
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 15 <exception> exception类的接口如下: namespace std{ //注意在名字空间域std中 class exception{ public: exception() throw() ; //默认构造函数 exception(const exception &) throw() ; //复制构造函数 exception &operator=(const exception&) throw() ; //复制赋值操作符 virtual ~exception() throw() ; //析构函数 virtual const char* what() const throw() ; //返回一个C风格的字符串,目的是为抛出的异常提供文本 描述 }; }