Table of Contents
In this article, we will see how to catch all exceptions in C++.
Exception Handling in C++
An exception is an error condition that occurs when a program is running and causes it to crash. It can be due to accessing an out of index element from an array, dividing a number by 0, and more. There are various types of exceptions.
In C++, we can use the try
and catch
block to handle exceptions. If we believe some code can raise an exception, we place it in the try
block. If the exception occurs, it is caught in the catch
block which executes some alternative code. An exception can be explicitly thrown using the throw
keyword.
See the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int x = 0; try{ if(x == 0){ throw x;; } } catch(int a){ cout << "Exception Caught"; } return 0; } |
Output:
The above code demonstrates a simple case of exception handling in C++.
Catch All Exceptions in C++
As discussed earlier, there are many types of exceptions in C++. In the catch
block, we need to mention the type of exception it will catch. In our previous example, an int
exception was thrown using the throw
statement and in the catch
block, we mentioned that it will catch the int
exception.
We may encounter complicated exceptions at times and these may be thrown by the compiler due to some abnormal code. Therefore, it is necessary to know how to define a catch
block to catch all exceptions in C++.
To catch all the exceptions, we specify ellipses(...
) in the catch
block. This method will catch all types of exceptions in the program.
We implement this in the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { int x = 1; char a = 'a'; try{ if(x == 0){ throw x;; } if(a == 'a'){ throw a; } } catch(...){ cout << "Exception Caught"; } return 0; } |
Output:
In the above example, we used the catch(...)
block to catch all the exceptions.
Note: One thing to remember is that this method will catch all the language-level and other low-level exceptions. It will not catch exceptions like
Access_Violation
,Segmentation_Fault
, etc.
Also, it is not considered a good method to catch all exceptions. It is considered a good programming notion to catch all exceptions and deal with them individually. Apart from the fact that some extreme signals and exceptions may still crash the program, it is also difficult to know what error occurs in the program if all the exceptions are caught using catch(...)
.
Sometimes, people confuse catch(...)
with catch(std::exception)
. Both are different since the latter will only catch the exceptions of type std::exception
.
Conclusion
This tutorial demonstrated how to catch all exceptions in C++. First, we discussed some basics of exception handling followed by how to catch all exceptions using catch(...)
and prevent the program from terminating unexpectedly. However, due to valid reasons, it is considered a good approach to all exceptions separately.
That’s all about how to catch all exceptions in C++.