0% found this document useful (0 votes)
30 views4 pages

Exception Handling in C++

The document explains exception handling in C++, detailing two types of exceptions: synchronous and asynchronous. It describes the use of try, catch, and throw keywords for managing exceptions, highlighting the advantages of this approach over traditional error handling. Key benefits include improved code readability, selective exception handling by functions, and the ability to group error types.

Uploaded by

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

Exception Handling in C++

The document explains exception handling in C++, detailing two types of exceptions: synchronous and asynchronous. It describes the use of try, catch, and throw keywords for managing exceptions, highlighting the advantages of this approach over traditional error handling. Key benefits include improved code readability, selective exception handling by functions, and the ability to group error types.

Uploaded by

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

Exception Handling in C++

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.
C++ try and catch
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.
Syntax of try-catch in C++

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.
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.
Why do we need Exception Handling in C++?
The following are the main advantages of exception handling over traditional
error handling:
1. Separation of Error Handling Code from Normal Code: There are always
if-else conditions to handle errors in traditional error handling codes.
These conditions and the code to handle errors get mixed up with the
normal flow. This makes the code less readable and maintainable. With
try/catch blocks, the code for error handling becomes separate from the
normal flow.

2. Functions/Methods can handle only the exceptions they choose: A


function can throw many exceptions, but may choose to handle some of
them. The other exceptions, which are thrown but not caught, can be
handled by the caller. If the caller chooses not to catch them, then the
exceptions are handled by the caller of the caller.
In C++, a function can specify the exceptions that it throws using the
throw keyword. The caller of this function must handle the exception in
some way (either by specifying it again or catching it).

3. Grouping of Error Types: In C++, both basic types and objects can be
thrown as exceptions. We can create a hierarchy of exception objects,
group exceptions in namespaces or classes, and categorize them
according to their types.
Examples of Exception Handling in C++
The following examples demonstrate how to use a try-
catch block to handle exceptions in C++.

Example: The example below demonstrates throwing


exceptions in C++.

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