Exceptions
Contents
General Exception
We can use the "..." to catch an exception of any type .File: except5.cpp
#include <iostream> #include <fstream> using namespace std; int main() { try { throw "Some error" ; } catch ( int& e1 ) { cout << "Integer Exception" << endl ; } catch( ... ) { cout << "Default Handler." << endl ; } return 0; }
Output: $ g++ except5.cpp ; ./a.exe Default Handler. The notation catch( ... ) means the catch can handle any type. We can also catch other specific exceptions as shown in the code at the same time. This may be useful if we are calling other functions that may throw some exception but we are not sure of the type. It also serves as a fallback mechanism to ensure that an exception does not escape the code. The "catch( ... )" must be the last handler if there are multiple catch statements. We cannot obtain any information from this handler as the object being thrown is not known at the handler.