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

C++ Exception Handling Basics

The document discusses exception handling in programming, particularly in C++, highlighting the distinction between logical and syntactic errors, as well as runtime exceptions. It explains the mechanism of handling exceptions using the keywords try, throw, and catch, detailing how to detect, report, and manage exceptions. Additionally, it covers synchronous and asynchronous exceptions, along with the structure for implementing multiple catch statements for various exception types.

Uploaded by

Pardeep
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)
7 views6 pages

C++ Exception Handling Basics

The document discusses exception handling in programming, particularly in C++, highlighting the distinction between logical and syntactic errors, as well as runtime exceptions. It explains the mechanism of handling exceptions using the keywords try, throw, and catch, detailing how to detect, report, and manage exceptions. Additionally, it covers synchronous and asynchronous exceptions, along with the structure for implementing multiple catch statements for various exception types.

Uploaded by

Pardeep
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

Exception Handling

It is very rare that a program works correctly first time. It might have bugs. The two
most common types of bugs are logical errors and syntactic errors.

The logic errors occur due to poor understanding of the problem and solution
procedure.

The syntactic errors arise due to poor understanding of the language itself.

We can detect these errors by using exhaustive debugging and testing procedures.

There are some other problems other than logic or syntax error known as
exceptions. Exceptions are run time anomalies or unusual conditions that a program
may encounter while executing.

Anomalies might include conditions such as division by zero, access to an array


outside of its bounds or running out of memory or disk space. When a program
encounters an exceptional condition, it is important that it is identified and dealt.
ANSI C++ provides built in language features to detect and handle exceptions which
are basically run time errors.

Basics of Exception Handling:


Exceptions are of two kinds namely synchronous exceptions and asynchronous
exceptions.

Errors such as ‘out of range index’ and ‘over flow’ belong to the synchronous
exceptions.

The errors that are caused by event beyond the control of the program (such as
keyboard interrupts) are called asynchronous exceptions.

The proposed exception handling mechanism in c++ is designed to handle only


synchronous exceptions.

The purpose of the exception handling mechanism is to detect and report an


exception circumstance so that appropriate action can be taken. It performs the
following tasks
1. Find the problem (hit the exception)
2. Inform that an error has occurred(throw the exception)
3. Receive the error information (catch the exception)
4. Take corrective actions(handle the exception)

The error handling code basically consists of two segment, one to detect
errors and to throw exceptions, and the other to catch the exception and to
take appropriate actions.

Exception Handling Mechanism


C++ exception handling mechanism is basically built upon the three keywords
namely try, throw and catch. The keyword try is used to preface a block of
statements which may generate exceptions. This block of statement is known as
try block. When an exception is detected it is thrown using a throw statement in
try block. A catch block defined by the keyword catch that catches the exception
thrown by throw statement in the try block and handle sit appropriately.

The general form of these two blocks are as follow:


try
{
…..
throw exceptions; //block of statements which detects and
…… throws an exception
…..
}
catch(type arg) // catches the exception
{
……
….. // block of statements that handles the
…… exception
}

When try blocks throws an exception, the program control leaves the try block and
enters the catch statement of the catch block.

Note: Exceptions are objects used to transmit information about a problem.

 If the types of object thrown matches the arg type in the catch statement, then
catch block is executed for handling the exception.
 If they do not match, the program is aborted with the help of abort() function
which is invoked by default.
 When no exception is detected and thrown, the control goes to the statement
immediately after the catch block (The catch block is skipped).

Example: Try block throwing an exception.

#include <iostream>
using namespace std;
int main()
{
try
{
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
return 0;
}

Throwing Mechanism
When an exception that is desired to be handled is detected, it is thrown using the
throw statement in one of the following forms:

throw (exceptions);

throw exceptions;

throw; used for rethrowing an exception

When an exception is thrown, it will be caught by the catch statement associated


with the try block. That is the control exists the current try block, and is transferred
to the catch block after that try block.

Catching Mechanism:
Code for handling exception is included in the catch blocks. A catch block look like
a function definition and is of the form

Catch (type arg)

{
//statements for

// managing exceptions

}
The type indicates the type of exception that catch block handles. The parameter
argument is an optional parameter name. The exception handling code is placed
between two braces. The catch statement catches an exception whose type
matches with type of catch argument. When it is caught, the code in the catch
block is executed.

Multiple catch Statements:


It is possible that a program segment has more than one condition to throw an
exception. In such cases, we can associate more than one catch statement with a
try (like the statement in a switch statement) as shown below:

try

// try block

catch(type1 arg)

//catch block 1

catch(type2 arg)

//catch block2

…….
……

catch(typeN arg)

//catch block n)

When an exception is thrown, the exception handler are searched in order for an
appropriate match. The first handler that yields a match is executed. When no match
is found, the program is terminated.

You might also like