You are on page 1of 6

Prepared By V.L.

Kartheek

UNIT-V
EXCEPTION HANDLING

Syllabus:-
➢ Benefits of exception handling,
➢ Throwing an exception,
➢ The try block,
➢ Catching an exception,
➢ Exception objects,
➢ Exception specifications,
➢ Stack unwinding,
➢ Rethrowing an exception,
➢ Catching all exceptions.
Prepared By V.L.Kartheek

Exception :-
➢ An exception is a problem that arises during the execution of a program.
➢ A C++ exception is a response to an exceptional circumstance that arises while a program is
running, such as an attempt to divide by zero, array out of bounds exception.

There are two types of exceptions:

1. Synchronous exceptions:-

➢ Errors such as: out of range index and overflow fall under the category of synchronous type
exceptions.

2. Asynchronous exceptions:-

➢ Those errors that are caused by events beyond the control of the program are called asynchronous
exceptions.
➢ Hardware failures are examples of asynchronous exceptions.

Benefits of exception handling:-

1) Separation of Error Handling code from Normal Code

2) Functions/Methods can handle any exceptions they choose

3) Grouping of Error Types

Principles of Exception Handling


(0r)
Keywords for handling Exceptions
(or)
Exception Handling Mechanism

➢ An exception is an object. It is sent from the part of the program where an error occurs to the
part of the program that is going to control the error.
Principles of exception handling:
1) Find and report an exception object
2) Throw an exception object using throw.
3) Capture an exception using catch and process the exception.
Prepared By V.L.Kartheek

(Fig) : Exception Handling Mechanism

Example for Handling Divided by Zero Exception:-

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout << "enter first number" << endl;
cin >> a;
cout << "enter second number" << endl;
cin >> b;
try //try block
{
if(b == 0)
{
throw(b); //throwing an exception
} // Here b is an exception Object to be thrown
else
{
cout<<”a/b=”<<a/b;
}
}
catch(int x) //Catching an exception
{
cout << "Division is not possible by "<<x;
}
}
Prepared By V.L.Kartheek

Exception objects:-

➢ The exception object which is thrown gets its type from the static type of the throw expression
ignoring any const and volatile qualifiers.
Example:-

try //try block


{
if(b == 0)
{
throw(b); //throwing an exception
} // Here b is an exception Object to be thrown
else
{
cout<<”a/b=”<<a/b;
}
}
Exception specifications:-

➢ Exception specification tells about what type of exception is thrown by throw statement.
➢ Always, the type of exception object and catching object must be same otherwise it will give
You a type mismatch error.

Example:-

try //try block


{
if(b == 0)
{
throw(b); //throwing an exception
} // Here b is an exception Object to be thrown
else
{
cout<<”a/b=”<<a/b;
}
}
catch(int x) //Catching an exception
{
cout << "Division is not possible by "<<x;
}
}

➢ In the above example the type of b and x both are same otherwise it will give you a type
Mismatch error.

Stack unwinding:-
➢ The stack unwinding is a process where the function call stack entries are removed at
runtime.
➢ To remove stack elements, we can use exceptions.
➢ If an exception is thrown from the inner function, then all of the entries of the stack is removed,
and return to the main invoker function.
Prepared By V.L.Kartheek

Example:-

f3() Start
f2() Start
f1() Start
Caught Exception:
f3() End
➢ Stack entry is removed from the function call stack (because it f1(),f2() and f3() doesn’t
contain exception handler for the thrown exception)

Rethrowing an exception:-

➔ Rethrowing an expression from within an exception handler can be done by calling throw, by
itself, with no exception.
➔ This causes current exception to be passed on to an outer try/catch sequence.
➔ An exception can only be rethrown from within a catch block.
➔ When an exception is rethrown, it is propagated outward to the next catch block.

Example:-

void exceptionFunction()
{
try // try block - inside Function
{
throw 0; //throw exception : In function
}
catch (int i)
{
cout << "\nIn Function : Wrong Input :" << i; //re throw exception : out of the function
throw;
}
}

Catching all exceptions:-


Catching Multiple Exceptions:-

➢ It is also possible to handle multiple exceptions using a single or default catch statement.
Syntax:-
try
{
//try block
}
catch(…)
{
//catch block
}
Prepared by V.L.Kartheek

Handling Multiple Exceptions using a single Catch statement:-


#include<iostream.h>
#include<conio.h> void
test(int x)
{
try {
if (x > 0)
throw x;
else
throw 'x';
}
Catch
{
cout << " Exception caught" << x;
}
}
void main() {
cout << "Testing multiple catches\n:";
test(10);
test(0);
getch();
}
Output:-
Testing multiple catches
Exception caught

You might also like