0% found this document useful (0 votes)
4 views6 pages

Exception-Handling-in-CPP

The document discusses exception handling in C++, which allows for the management of runtime errors to maintain the normal flow of an application. It introduces the keywords 'try', 'catch', and 'throw' for handling exceptions, along with examples demonstrating their usage. Additionally, it outlines common exception classes derived from std::exception and highlights the advantages of using exception handling in C++ programming.

Uploaded by

jigarghoghari59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Exception-Handling-in-CPP

The document discusses exception handling in C++, which allows for the management of runtime errors to maintain the normal flow of an application. It introduces the keywords 'try', 'catch', and 'throw' for handling exceptions, along with examples demonstrating their usage. Additionally, it outlines common exception classes derived from std::exception and highlights the advantages of using exception handling in C++ programming.

Uploaded by

jigarghoghari59
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Programming

Topperworld.in

Exception Handling

 Exception Handling in C++ is a process to handle runtime errors. We


perform exception handling so the normal flow of the application can be
maintained even after runtime errors.
 One of the advantages of C++ over C is Exception Handling. Exceptions are
runtime anomalies or abnormal conditions that a program encounters
during its execution
 In C++, exception is an event or object which is thrown at runtime. All
exceptions are derived from std::exception class. It is a runtime error which
can be handled. If we don't handle the exception, it prints exception
message and terminates the program.
 Before go to deep let’s understand about the types of error:
Errors can be broadly categorized into two types. We will discuss them
one by one.

1. Compile Time Errors

2. Run Time Errors

Compile Time Errors – Errors caught during compiled time is called


Compile time errors. Compile time errors include library reference, syntax
error or incorrect class import.

Run Time Errors - They are also known as exceptions. An exception caught
during run time creates serious issues.

Errors hinder normal execution of program. Exception handling is the


process of handling errors and exceptions in such a way that they do not
hinder normal execution of the system. For example, User divides a number
by zero, this will compile successfully but an exception or run time error will
occur due to which our applications will be crashed. In order to avoid this
we'll introduce exception handling technics in our code.

©Topperworld
C++ Programming

 Exception Handling Keywords


In C++, we use 3 keywords to perform exception handling:

o try
o catch
o throw

 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.
 The catch statement allows you to define a block of code to be executed
if an error occurs in the try block.
Syntax:
try {
// Block of code to try
throw exception;
}
catch () {
// Block of code to handle errors
}

Let's take a simple example to understand the usage of try, catch and throw.

Below program compiles successfully but the program fails at runtime, leading
to an exception.

#include <iostream>#include<conio.h>

using namespace std;

int main()

©Topperworld
C++ Programming

int a=10,b=0,c;

c=a/b;

return 0;

The above program will not run, and will show runtime error on screen, because
we are trying to divide a number with 0, which is not possible.

How to handle this situation? We can handle such situations using exception
handling and can inform the user that you cannot divide a number by zero, by
displaying a message.

 Using try, catch and throw Statement

Now we will update the above program and include exception handling in it.

#include <iostream>

#include<conio.h>

using namespace std;

int main()

int a=10, b=0, c;

// try block activates exception handling

try

©Topperworld
C++ Programming

if(b == 0)

// throw custom exception

throw "Division by zero not possible";

c = a/b;

catch(char* ex) // catches exception

cout<<ex;

return 0;

Output:

Division by Zero not possible

In the code above, we are checking the divisor, if it is zero, we are


throwing an exception message, then the catch block catches that
exception and prints the message.

©Topperworld
C++ Programming

Doing so, the user will never know that our program failed at runtime, he/she
will only see the message "Division by zero not possible".

This is gracefully handling the exception condition which is why exception


handling is used.

 Exception Classes
In C++ standard exceptions are defined in <exception> class that we can use
inside our programs. The arrangement of parent-child class hierarchy is shown
below:

All the exception classes in C++ are derived from std::exception class. Let's see
the list of C++ common exception classes.

Exception Description

std::exception It is an exception and parent class of all standard C++


exceptions.

©Topperworld
C++ Programming

std::logic_failure It is an exception that can be detected by reading a code.

std::runtime_error It is an exception that cannot be detected by reading a


code.

std::bad_exception It is used to handle the unexpected exceptions in a c++


program.

std::bad_cast This exception is generally be thrown by dynamic_cast.

std::bad_typeid This exception is generally be thrown by typeid.

std::bad_alloc This exception is generally be thrown by new.

Advantage of Exception Handling

It maintains the normal flow of the application. In such case, rest of the
code is executed even after exception.

©Topperworld

You might also like