Exception¶
- Exception type
C++
class VectorIndexError
{
public:
VectorIndexError(int v):m_badValue(v){}
VectorIndexError({}
void diagnostic(){
cout<<"index"<< m_badValue <<"out of range!";
}
private:
int m_badValue;
}
throw
raises exception.
- Try blocks can select type of exceptions
C++
try{
func();
} catch(VIE v){ // take a single argument
cout<< "8\n";
} catch (...){ // others
cout<< "7\n";
}
it can re-raise exceptions
- Hierarchy of exception types
C++
class MathErr{
...
virtual void diagnostic();
};
class OverflowErr : public MathErr {...}
class UnderflowErr : public MathErr {...}
a catch of parent Exception Type can catch its child type.
C++
try{
throw VIEE(idx);
}catch (VIE v){
cout<< "7\n";
}catch (...){
cout<< "6\n";
}
// output 7
try{
throw VIEE(idx);
}catch (VIEE v){
cout<< "8\n";
}catch (VIE v){
cout<< "7\n";
}catch (...){
cout<< "6\n";
}
// output 8
try{
throw VIEE(idx);
}catch (VIE v){
cout<< "8\n";
}catch (VIEE v){ // this expression is useless
cout<< "7\n";
}catch (...){
cout<< "6\n";
}
// output 8
-
Standard Library Exception
-
Failure in C'tor