0% found this document useful (0 votes)
5 views33 pages

Exception Handling(1)

The document provides an overview of exception handling in C++, explaining the types of errors, including syntax and logic errors, and the concept of exceptions as runtime anomalies. It details the use of try, catch, and throw keywords for managing exceptions, along with the properties and implementation of exception handling, including multiple catch blocks and a catch-all block. The conclusion emphasizes that exception handling enhances program reliability by managing runtime errors effectively.

Uploaded by

flankerisback
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)
5 views33 pages

Exception Handling(1)

The document provides an overview of exception handling in C++, explaining the types of errors, including syntax and logic errors, and the concept of exceptions as runtime anomalies. It details the use of try, catch, and throw keywords for managing exceptions, along with the properties and implementation of exception handling, including multiple catch blocks and a catch-all block. The conclusion emphasizes that exception handling enhances program reliability by managing runtime errors effectively.

Uploaded by

flankerisback
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/ 33

Object Oriented Programming

(24ECE2101)

Batch 2024, ECE

Teaching Team

Dr Deepti Prit Kaur Dr Meenu Garg


Group X Group Y

1
Introduction to Exception Handling

• The two common types of errors that we often


encounter are syntax errors and logic errors.
• While logic errors occur due to poor understanding of
problem and its solution; syntax errors, on the other
hand, arise due to poor understanding of the language.
• However, such errors can be detected by exhausting and
debugging and testing procedures.
• However, many a time, we come across some peculiar
problems which are often categorized as exceptions.
Introduction to Exception Handling

• Exceptions are run-time anomalies or unusual conditions such as


divide by zero, accessing arrays out of its bounds, running out of
memory or disk space, overflow ,and underflow that a program
may encounter during execution.
• To handle such exceptional situations, a new feature of
exception handling has been added to ANSI C++ that helps the
programmers to identify and effectively deal with these run-time
errors.
Exception and it’s Types
What is a C++ Exception?
An exception is an unexpected problem that arises during the execution of a
program our program terminates suddenly with some errors/issues. Exception
occurs during the running of the program (runtime).

Types of C++ Exception

There are two types of exceptions in C++


1.Synchronous: Exceptions that happen when something goes wrong because of a
mistake in the input data or when the program is not equipped to handle the
current type of data it’s working with, such as dividing a number by zero.

2.Asynchronous: Exceptions that are beyond the program’s control, such as disc
failure, keyboard interrupts, etc.

4
EXCEPTION HANDLING

• Exceptions provide a way to react to exceptional conditions in


programs by transferring program control to special functions
called handlers.
• The main aim of exception handling is to detect and report an
exceptional condition(s), so that appropriate action can be taken
to deal with it.
• C++ provides an inbuilt feature for Exception Handling. It can be
done using the following specialized keywords: try, catch, and
throw with each having a different purpose.
Try, Catch and Throw

Try block This block is a group of


statements that may generate an
exception.

When an exception is generated, it


is thrown using the keyword throw.
The throw keyword invokes the
exception handling routine.

Catch block This block catches the


exception thrown from the try
block and handles it in the way
specified by the statements in the
catch block.
Try, Catch and Throw
1. try in C++
The try keyword represents a block of code that may throw an exception placed
inside the try block. It’s followed by one or more catch blocks.
If an exception occurs, try block throws that exception.

2. catch in C++
The catch statement represents a block of code that is executed when a particular
exception is thrown from the try block. The code to handle the exception is written
inside the catch block.

3. throw in C++
An exception in C++ can be thrown using the throw keyword. When a program
encounters a throw statement, then it immediately terminates the current function
and starts finding a matching catch block to handle the thrown exception.
Note: Multiple catch statements can be used to catch different type of exceptions
thrown by try block.
The try and catch keywords come in pairs: We use the try
block to test some code and If the code throws an exception
we will handle it in our catch block.
EXCEPTION HANDLING
Handling of divide by 0 Exception
Implementation
Implementation
You can also use the throw keyword to output a reference number, like a
custom error number/code for organizing purposes (505 in our example):
Example explained

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 be greater than 18), the catch block is skipped:
Handling of Exceptions
Multiple Catch Blocks
Multiple Catch Blocks

• It is possible to associate more than one


catch statement with a single try block.
This is usually done when a program
segment has more than one condition to
throw as an exception.
• In such cases, when an exception is
thrown, the exception handlers are
searched to find an appropriate match.
• The first catch block that matches the
type of the exception thrown is executed.
• After execution, the program control goes
to the first statement after the last catch
block for that try block.
Multiple Catch Blocks

• This means that all other catch blocks are ignored.


• However, if no match is found, then the program is
terminated using the default abort().
Implementation of Multiple Catch Blocks
Guess the output?
Properties of Exception Handling in
C++: Property 1
Property 1
There is a special catch block called the ‘catch-all’ block, written as catch(…),
that can be used to catch all types of exceptions.
• In large software programs, often, it is difficult to anticipate all types
of possible exceptional conditions.
• Therefore, the programmer may not be able to write a different
handler (catch block) for every individual type of exception.
• In such situations, a better idea is to write a handler that would catch
all types of exceptions.
• This is something similar to the default case in a switch statement.
Implementation of Catch all Exceptions
Implementation of Catch all
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:
Guess the output?
Property 2
Implicit type conversion doesn’t happen for primitive types.

In the following program, ‘a’ is not implicitly converted to int.


Property 3
If an exception is thrown and not caught anywhere, the program terminates
abnormally.
Example
In the following program, a char is thrown, but there is no catch block to catch the char.
Property 4
In C++, try/catch blocks can be nested. Also, an exception can be re-thrown using “throw; “.
Example
The following program shows try/catch blocks nesting.

A function can also re-throw a function using the same


“throw; ” syntax. A function can handle a part and ask the
caller to handle the remaining.
Property 5

When an exception is thrown,


all objects created inside the
enclosing try block are
destroyed before the control is
transferred to the catch block.
Example
The following program demonstrates
the above property.
Conclusion

• Exception handling in C++ is used to handle unexpected


happening using “try” and “catch” blocks to manage the
problem efficiently.
• This exception handling makes our programs more reliable
as errors at runtime can be handled separately and it also
helps prevent the program from crashing and abrupt
termination of the program when error is encountered.
Quiz Time

Which of the following is the most general exception


handler that catches exception of ‘any type’?

a. catch(…)
b. catch()
c. catch(std::any_exception)
d. catch(std::exception)

Answer: a
Quiz Time

What is the primary advantage of using try/catch blocks over


traditional error-handling methods in C++?
A) It increases the execution speed of the program
B) It separates error-handling code from normal code,
improving readability and maintainability
C) It eliminates the need for error handling entirely
D) It ensures that all errors are caught automatically

Answer: b

29
Quiz Time

What is the purpose of exception handling in


programming?
A) To handle runtime errors and prevent program crashes
B) To optimize code execution speed
C) To avoid using conditional statements
D) To convert all errors into warnings

Answer: a

30
Quiz Time

What is the correct syntax for handling exceptions in


C++?
a) try { } throw { } catch { }
b) try { } catch { } throw { }
c) try { } catch { }
d) throw { } try { } catch { }

Answer: c

31
Quiz Time

a) Hello
b) Caught exception: 5
c) Compilation error
d) No output

Answer: b
Thanks

33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy