Exception Handling
Exception Handling
Exception Handling
Exception Handling
Exception handling: Introduction, C-Style Handling of Error-generating Code, C++ Style
Solution - the try/throw/catch Construct.
Overview of Error and Exception:
Errors are the problems that occur in
the program due to an illegal operation performed by the user or by the fault
of a programmer. Errors are also termed as bugs or faults. Errors can stop
normal flow of program execution, produce inaccurate results, or even cause
the program to crash.
Effective error handling is essential for addressing these errors and ensuring
the program's stability and dependability. The errors can be broadly
categorized into three main types: syntax errors, runtime errors, and logic
errors.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 5;
// Dividing the number a by zero, so the program will compile easily
// but run time error will be generated.
cout << a / 0;
return 0;
}
Output:
test.cpp: In function 'int main()':
test.cpp:11:14: warning: division by zero
cout << a / 0;
~~^~~
Logic Errors:
Logic errors occur when the code compiles and runs without
errors but does not produce the expected or desired output. These errors can
be tricky to identify because the program behaves as intended, but the
outcome is incorrect due to errors in the logical flow or calculations in the
code. Logic errors often require careful debugging and code inspection to
identify and fix.
Example:
int main() {
int radius = 5;
double area = 3.14 * radius * radius; // Incorrect formula for calculating
area of a circle
cout << "The area is: " << area << endl;
return 0; }
What is Exception?
Exceptions are also called as run-time errors, is an abnormal condition
that a program may encounter during program execution. Many a time, your
program looks error-free during compilation. But, at the time of execution
(runtime), some unexpected errors or unwanted
events or objects occurred (come to the surface), and the program prints
an error message and stops executing. Such unexpected happenings are
called exceptions.
Example:
int a = 8/0;
In the above example, the program will show an error during runtime
because dividing by zero is undefined.
Exceptions occur for numerous reasons, including invalid user input, code
errors, device failure, the loss of a network connection,
insufficient memory to run an application, a memory conflict with another
program, a program attempting to divide by zero operation, a program
attempting to Access to an array out of its bounds or a user attempting to
open files that are unavailable.
What are the types of exceptions?
Similar to errors, exceptions are also
of two types. They are as follows:
Synchronous exceptions: The exceptions which occur during the
program execution due to some fault in the input data. For example:
Errors such as out of range, overflow, division by zero
Asynchronous exceptions: The exceptions caused by events or
faults unrelated (external) to the program and beyond the control of
the program. For Example: Key board failures, hardware disk failures
Exception Description
std::exceptio It is an exception and parent class of all
n standard C++ exceptions.
std::logic_fail It is an exception that can be detected by
ure reading a code.
std::runtime_ It is an exception that cannot be detected by
error reading a code.
std::bad_exce It is used to handle the unexpected exceptions
ption in a c++ program.
std::bad_cast This exception is generally be thrown
by dynamic_cast.
std::bad_type This exception is generally be thrown
id by typeid.
std::bad_alloc This exception is generally be thrown by new.
try: The block of code defined under this statement is being tested for
runtime errors (The keyword try is used to preface a block of statements
which may generate exceptions). If exceptions occur that may throw.
Syntax of try statement:
try
{
statement 1;
statement 2;
}
throw: When an exception is detected, it is thrown using a throw statement
in the try block. It means, transfers control to the nearest catch block that
matches the type of the thrown exception, enabling error handling.
Syntax of throw statement
throw (excep);
throw excep;
throw; // re-throwing of an exception
catch: A catch block defined by the keyword ‘catch’ catches the exception
and handles it appropriately. The catch block that catches an exception must
immediately follow the try block that throws the exception.
catch(Exception_name exception_1)
{
// code to handle exceptions
}
Complete syntax:
try
{
// protected code
throw exception;
}
catch(Exception_name exception_1)
{
// code to handle exceptions
}
The Exception_name is the name of the exception to be caught. The
exception_1, is defined names. They are referring to the exceptions.
Every try block is followed by the catch Block. The throw Statement throws
an exception, which is caught by the catch block. The catch block cannot be
used without the try block.
As soon as an exception is found, the throw statement inside the try block
throws an exception (a message for the catch block that an error has
occurred in the try block statements). Only errors occurring inside the try
block are used to throw exceptions. The catch block receives the exception
that is sent by the throw block. The general form of the statement is as per
the following figure.