问题:
为什么将扑捕获的异常再次扔出去??
答:工程中可以重新解释异常
开发自己的统一异常类型,方便快速查找问题 !!(因为第三方库没有源代码的,只有输入输出的说明以及函数原型)
类类型的异常处理
类对象异常,catch语句采用引用作为参数,避开拷贝构造
void Myfunc() { try { func(110); } catch(int i) { switch(i) { case -1: throw Exception(-1, "Invalid Paremeter"); //直接调用构造函数来生成对象的操作 break; case -2: throw Exception(-2, "Runtime Exception"); break; case -3: throw Exception(-3, "Timeout Exception"); break; } } } try { Myfunc(); } catch(const Exception& e) { qDebug() << "ID = " << e.ID(); qDebug() << "Message =" << e.message(); } catch(const Base& b) //子类在上,父类在下的原则 { qDebug() << "catch(const Base& b)"; } #include <stdexcept> template < typename T, int N > T& Array<T, N>::operator[] (int index) { if( (0 <= index) && (index < N) ) { return m_array[index]; } else { throw out_of_range("T& Array<T, N>::operator[] (int index)"); } }