100% found this document useful (1 vote)
7 views8 pages

Exception Handling in C++

C++ exceptions are runtime anomalies that can disrupt program execution, and exception handling allows for control transfer to manage these issues. There are two types of exceptions: synchronous, caused by input errors, and asynchronous, which are beyond program control. Exception handling improves code readability and maintainability by separating error handling from normal code flow and allowing functions to selectively manage exceptions.

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
100% found this document useful (1 vote)
7 views8 pages

Exception Handling in C++

C++ exceptions are runtime anomalies that can disrupt program execution, and exception handling allows for control transfer to manage these issues. There are two types of exceptions: synchronous, caused by input errors, and asynchronous, which are beyond program control. Exception handling improves code readability and maintainability by separating error handling from normal code flow and allowing functions to selectively manage exceptions.

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/ 8

Exception Handling in C++

What is a C++ Exception?


Exceptions are runtime anomalies or abnormal conditions that a program
encounters during its execution. 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). The process of handling these exceptions is called exception
handling. Using the exception handling mechanism, the control from one part
of the program where the exception occurred can be transferred to another
part of the code.
So basically using exception handling in C++, we can handle the exceptions so
that our program keeps running.

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.
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.
Working with Files

Now change r(read) to w(write)


Type something in the same file
Reading from a file by changing w mode to r mode

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