0% found this document useful (0 votes)
11 views10 pages

Exception Handling

The document outlines the exception handling mechanism in programming, detailing the process of detecting, reporting, and managing exceptions through try and catch blocks. It explains how exceptions are thrown by functions and caught by catch blocks, allowing for appropriate error handling. Additionally, it discusses the possibility of multiple catch statements to handle various exception types within a single try block.

Uploaded by

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

Exception Handling

The document outlines the exception handling mechanism in programming, detailing the process of detecting, reporting, and managing exceptions through try and catch blocks. It explains how exceptions are thrown by functions and caught by catch blocks, allowing for appropriate error handling. Additionally, it discusses the possibility of multiple catch statements to handle various exception types within a single try block.

Uploaded by

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

Exception handling Mechanism

The purpose of the exception handling is to provide a mechanism to


detect and report
the ‘exceptional circumstances’ in the program so that appropriate
action can be taken.

This process involves separate error handling code that performs the
following tasks
 Identify the problem (Hit the exception)
 Report that an error has occurred (Throw the exception)
 Receive the error information (Catch the exception)
 Take corrective actions (Handles the exception)
Exception Handling Model

try block

Detects and throws


an exception

Exception object

catch block

Catches and handles


an exception

2
 The keyword try is used to preface a block of statements
surrounded by braces which may be generating
exceptions; this block of statements is known as try
block

 When an exception is detected, it is thrown using a


throw statement in the try block

 A catch block is defined by the keyword catch catches


the exception thrown by the throw statement in the try
block and handles it appropriately
Exception Handling: Format
try
{
……
throw exception; // block of statements
// which detects and
// throws an exception
}

catch (type arg) // catches exception


{
……….
………. // block of statements that
………. // handles the exception
}
Throw point

Function that causes


an exception

Throw exception Invoke


function
try block

invokes a function that


contains an exception

catch block

Catches and handles


an exception

5
 Most often, exceptions are thrown by functions that are invoked
from within the try block
 The point at which the throw block is executed is called the throw
point

 Once an exception is thrown to the catch block, control cannot


return to the throw point
Invoking Function that Generates Exception
inside the try block
void divide (int x, int y, intz){
inside the function
cout << " inside the function \n “;
Result = -3
if (( x-y) !=0)
Caught the exception
{ int R = z/(x–y) ;
cout<< " Result = “ << R << “\n”; }
else // there is a problem
{throw (x-y);// throw point }
}
void main() {
try {cout<< ” inside the try block \n”;
divide(20,10,30); // Invoke
divide
divide(30,30,60); // Invoke divide
}
catch (int i) {
cout<< “ caught the exception \n
“;
7
}
Catching Mechanism

A catch block looks 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 arg is an optional
8
Multiple Catch Statements
 It is possible that a program
segment has more than one
condition to throw an exception;
such cases, you can associate more
than one catch statements with a
try as shown in the following code
try
{ // try block }
catch (type1 arg)
{ // catch block1 }
9
10

void Xhandler(int
test) { void main() {
try{ cout << "Start\n";
if(test) throw test;Xhandler(1);
Xhandler(2);
else throw "Value
is zero"; Xhandler(0);
Xhandler(3);
}
cout << “End"; }
catch(int i) {
cout << "Caught
Exception #: " << i; Start

You might also like