Error handling
in C programming
is not natively supported, but programmers can access low-level error information through return values. When an error occurs, C functions typically return a value such as -1
or NULL
. Programmers can then check these values and take appropriate action. However, using control structures like if-else statements
to handle errors can be unreliable. For example, a function that calculates the quotient of a division operation may return 0
when a division-by-zero error occurs. But since 0 is also a valid quotient, it's hard for the calling program to determine if an error has occurred. In these cases, more advanced error handling
techniques are necessary.
C++
offers an exception handling mechanism to handle errors or unexpected events that occur during program execution. These exceptions are run-time errors or abnormal conditions that the program encounters. Exception handling inC++
is an advantage overC
, as it provides a structured way to handle such situations.
C++
provides the following specialized keywords
for this purpose.
- try : represents a block of code that contains success scenario code or it can throw an exception in case of error.
- catch : represents a block of code that is executed when a particular exception is thrown. It contains alternate scenario code.
- throw : Used to throw an exception.
The try and catch keywords come in pairs,If an exception is thrown from a function and there is no catch block to handle it, the program will crash with an error message indicating that the exception was not handled.
- Exceptions can be of any type such as
int
,float
,char*
, orclass objects
. - The
catch block
must be parameterized with the relevant data type of the exception thrown. - Multiple
catch blocks
with different parameters can be used and the appropriate one will execute based on the type of exception thrown. - A function can either handle exceptions by including local
try-catch blocks
or throw them to be handled by the caller.
A catch block
with the ellipsis (...) will catch any type of exception thrown. It can be used as a default handler for any unhandled exceptions. Example: catch(...)
Default exception handling.
In C++
, we can use classes to create custom exception types that can be thrown and caught in a more readable and organized way. Instead of throwing raw integers
or strings
, we can create a class that represents a specific type of exception and throw an object
of that class
.
In
C++
, there are several built-in exception classes that can be used to handle specific types of errors. These classes are defined in the<stdexcept>
header file.
bad_alloc
: This exception is thrown by the new operator when memory allocation fails.
bad_cast
: This exception is thrown by the dynamic_cast operator when a cast is not valid.
bad_typeid
: This exception is thrown by the typeid operator when it is used with a null pointer.
- logic_error: This is a base class for other exception classes that indicate errors in program logic. invalid_argument: This exception is thrown when an invalid value is passed to a function.
underflow_error
: This exception is thrown when the result of an arithmetic operation is smaller than the smallest number that can be stored in a given numeric type.
length_error
: This exception is thrown when anobject's size
exceeds its allowed maximum range.out_of_range
: This exception is thrown when a value, such as asubscript
into anarray
, exceeds its allowed range of values.runtime_error
: This is abase class
for other exception classes that indicateexecution-time
errors.overflow_error
: This exception is thrown when the result of an arithmetic operation exceeds the largest number that can be stored in a given numeric type.