18.5 CATCHING ALL EXCEPTIONS

If the exception parameter is an ellipsis (…), the catch clause handles any type of exception, including a C exception. Such a handler must be the last handler for its try-block.

Problem: Write a program to demonstrate the use of catch with ellipsis.

Solution: See Program 18.2.

Program 18.2 Catch with ellipsis

// excep4.cpp#include<iostream>using namespace std;int main(){  int i=3,j=0,k =3;   cout<< “<---excep4.cpp--->” << endl ;   try     { if ( j == 0) throw 2.44 ;       k = i / j ;       cout << k << endl ;     }   catch (int k)     { cout << “ integer exception caught” << endl;     }   catch(…)     { cout <“ general exception caught” << endl;     } ;   cout << “val of k = ” << k << endl ;   return 0;}

If ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.