Exception Handling in C++
Exception Handling in C++
Exception handling in C++ consist of three keywords: try, throw and catch.
C++ TRY, THROW AND CATCH
The try statement allows you to define a block of code to be tested for errors
while it is being executed.
The throw keyword throws an exception when a problem is detected, which lets
us create a custom error.
In the catch block, we catch the error and do something about it. The catch
statement takes a parameter: in our example we use an int variable (myNum)
(because we are throwing an exception of int type in the try block (age)), to
output the value of age.
If no error occurs (e.g. if age is 20 instead of 15, meaning it will be be greater than
18), the catch block is skipped.
We use the try block to test some code: If the age variable is less than 18, we will
throw an exception, and handle it in our catch block.
In the catch block, we catch the error and do something about it. The catch
statement takes a parameter: in our example we use an int variable (myNum)
(because we are throwing an exception of int type in the try block (age)), to
output the value of age.
If no error occurs (e.g. if age is 20 instead of 15, meaning it will be greater than
18), the catch block is skipped.
You can also use the throw keyword to output a reference number, like a custom
error number/code for organizing purposes.
Handle Any Type of Exceptions (...)
If you do not know the throw type used in the try block, you can use the "three dots"
syntax (...) inside the catch block, which will handle any type of exception.
Throwing Standard Exceptions
When the standard exceptions cannot satisfy our requirement, we can create a
custom exception class. It is recommended to inherit standard exception in this
class to provide seamless integrity with library components though, it is not
compulsory.
Why do we need Exception Handling in C++?
Errors or abnormal conditions can also be handled without exception handling, like it is done
in C using conditional statements. But an exception handling provides the following
advantages over traditional error handling:
Separation of Error Handling Code from Normal Code: There are always if-else conditions
to handle errors in traditional error handling codes. These conditions and the code to
handle errors get mixed up with the normal flow. This makes the code less readable and
maintainable. With try and catch blocks, the code for error handling becomes separate
from the normal flow.
Functions/Methods can handle only the exceptions They choose: A function can
throw many exceptions but may choose to handle some of them. The other
exceptions, which are thrown but not caught, can be handled by the caller. If
the caller chooses not to catch them, then the exceptions are handled by the
caller of the caller.
In C++, a function can specify the exceptions that it throws using the throw
keyword. The caller of this function must handle the exception in some way
(either by specifying it again or catching it).
Grouping of Error Types: In C++, both basic types and objects can be thrown as
exceptions. We can create a hierarchy of exception objects, group exceptions in
namespaces or classes, and categorize them according to their types.
Best Practices of Exception Handling
Exception handling in C++ is a useful tool to make a robust program but there are several
common mistakes to avoid when implementing exception handling in our programs. So,
there are some best practices that should be followed in exception handling:
Don't use exceptions for regular control flow. Only use them for exceptional situations
(errors).
Catch exceptions by reference whenever possible to avoid copying overhead and
enable polymorphic selection.
Always catch exceptions by reference to a constant to avoid accidental modification.
Catching generic exceptions using catch(...) does not provide information about
exception type, so better to catch specific exceptions to handle errors
appropriately.
Not releasing resources (e.g., memory, file handles) after an exception can led to
resource leaks. Use proper cleanup mechanisms such as RAII or finally blocks.