C++ catch all exceptions

C++ catch all exceptions

In some situations, we may not be able to anticipate all types of exceptions and therefore also may not be able to design independent catch handlers to catch them.

In such circumstances, but we can force the catch statement to catch all the exceptions instead of a certain type alone. This could be achieved by defining the catch statement using ellipses as follows

catch(….)
{
// statements for processing
// all exceptions
}

CATCHING ALL Exceptions
#include <iostream>
using namespace std;
void test(int x)
{
try
{
if (x==0) throw x;
if (x==-1) throw 'x';
if (x==1) throw 1.0;
}
catch (....)
{
cout << "Caught an exception \n";
}
}
void main()
{
cout << "Testing generic catch \n";
test(-1);
test(0);
test(1);
}

the output is :
Testing Generic Catch
caught an exception
caught an exception
caught an exception

Note: that all the throws were caught by the catch(….) statement

It may be a good idea to use the catch(….) as a default statement along with other catch handlers so that it can catch all those exceptions which are not handled explicitly.

Note: Remember catch(…) should always be placed last in the list of handlers. placing it before other catch blocks would prevent those blocks from catching exception

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise