0% found this document useful (0 votes)
297 views

Exception Handling in C++

Exception handling in C++ allows programs to handle errors and unexpected events gracefully. Try blocks mark code that might throw exceptions. Catch blocks contain code to handle exceptions. When an exception occurs in a try block, program control jumps to the matching catch block. Common exceptions include divide-by-zero, out-of-bounds array access, and null pointer dereferences. Exception handling prevents program crashes, and allows the program to continue running or display an error message to the user.

Uploaded by

Srijana Shet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
297 views

Exception Handling in C++

Exception handling in C++ allows programs to handle errors and unexpected events gracefully. Try blocks mark code that might throw exceptions. Catch blocks contain code to handle exceptions. When an exception occurs in a try block, program control jumps to the matching catch block. Common exceptions include divide-by-zero, out-of-bounds array access, and null pointer dereferences. Exception handling prevents program crashes, and allows the program to continue running or display an error message to the user.

Uploaded by

Srijana Shet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Exception Handling in C++

Exception Handling Basics


• It is often easier to write a program by first
assuming that nothing incorrect will happen
• Once it works correctly for the expected
cases, add code to handle the exceptional
cases
• Exception handling is commonly used to
handle
error situations
– Once an error is handled, it is no longer an
error
Contd…
• An exception is any unusual event, either
erroneous or not, detectable by either hardware or
software, that may require special processing
• Without exception handling
– When an exception occurs, control goes to the operating
system, where typically
• an error message is displayed
• the program is terminated
• With exception handling
– Programs are allowed to trap exceptions
– There is a possibility to fix the problem and continuing
execution
What is Exception Handling?
• An exception is a problem that arises
during the execution of a program.
• A C++ exception is a response to an
exceptional circumstance that arises
while a program is running, such as an
attempt to divide by zero.
• Exceptions provide a way to transfer
control from one part of a program to
another.
How can Errors be divided?
• Errors can be broadly categorized into two
types.
• 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.
Exception Handling Mechanism
• In C++, exception handling proceeds by:
– Some library software or your code signals
that
something unusual has happened
– This is called throwing an exception
– At some other place in your program you
place the
code that deals with the exceptional case
– This is called handling the exception
Types of Exceptions
• This mechanism needs a separate error
handling code that performs the
following tasks:
– Find and hit the problem (exception)
– Inform that the error has occurred (throw
exception)
– Receive the error information (Catch the
exception)
– Take corrective actions (handle exception)
Contd…
• C++ exception handling is built upon three
keywords: try, catch, and throw.
– throw − A program throws an exception when a
problem shows up. This is done using
a throw keyword.
– catch − A program catches an exception with an
exception handler at the place in a program where
you want to handle the problem. The catch keyword
indicates the catching of an exception.
– try − A try block identifies a block of code for
which particular exceptions will be activated. It's
followed by one or more catch blocks.
• Assuming a block will raise an exception,
a method catches an exception using a
combination of the try and catch
keywords.
• A try/catch block is placed around the
code that might generate an exception.
Syntax:
Contd…
• If the try block throws an exception then
program control leaves the block and
enters into the catch statement of the
catch block.
• If the type of object thrown matches the
argument type in the catch statement, the
catch block is executed for handling the
exception.
try block
• The code which can throw any exception
is kept inside(or enclosed in) a try block.
Then, when the code will lead to any
error, that error/exception will get
caught inside the catch block.
catch block
• catch block is intended to catch the error
and handle the exception condition. We
can have multiple catch blocks to handle
different types of exception and perform
different actions when the exceptions
occur. For example, we can display
descriptive messages to explain why any
particular exception occurred.
throw statement
• It is used to throw exceptions to exception
handler i.e. it is used to communicate
information about error. A throw expression
accepts one parameter and that parameter
is passed to handler.
• throw statement is used when we explicitly
want an exception to occur, then we can
use throw statement to throw or generate
that exception.
Understanding Need of Exception
Handling
• 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.
Contd…
• 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
Contd…
• 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.
• 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".
Using Multiple catch blocks
Contd…
• The above program is self-explanatory, if
the value of integer in the array x is less
than 0, we are throwing a numeric value
as exception and if the value is greater
than 0, then we are throwing a character
value as exception.
• And we have two different catch blocks
to catch those exceptions.
Generalized catch block in C++
Contd…
• In the case above, both the exceptions
are being catched by a
single catch block.

• We can even have separate catch blocks


to handle integer and character
exception along with the
generalised catch block.
Standard Exceptions in C++
Contd…
Example
https://www.slideshare.net/AdilAslam4/ex
ception-handling-in-c-69353237

You might also like