Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Dept of EEE Object Oriented Programming

EXCEPTIONS

▰ Exception is an abnormal
condition.
▰ In programming , Exception is
an event that prevents the
program from continuing
normally while executing it.
▰ Exception is an error condition
that changes the normal flow
of control in a program. It is a
signal that some unexpected
condition has occurred in the
program.
2

S.Muralidharan 1
Dept of EEE Object Oriented Programming

Advantage of Exception Handling


▰ The core advantage of exception handling is to maintain the normal flow of the application. An
exception normally disrupts the normal flow of the application that is why we use exception handling.
Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;//exception occurs
Statement 5;
statement 6;
statement 7;
statement 8;

▰ Suppose there are 10 statements in your program and there occurs an exception at statement 4, the
rest of the code will not be executed i.e. statement 5 to 8 will not be executed. If we perform exception
handling, the rest of the statement will be executed. That is why we use exception handling in Java. 3

▰ C++ Exceptions:
▻ When executing C++ code, different errors can occur: coding errors made by the programmer,
errors due to wrong input, or other unforeseeable things.
▻ When an error occurs, C++ will normally stop and generate an error message. The technical term
for this is: C++ will throw an exception (error).
▰ C++ try and catch:
▻ Exception handling in C++ consists of three keywords: try, throw and catch:
▻ The try statement allows you to define a block of code to be tested for errors while it is being
executed.
▻ The throw keyword throws an exception when a problem is detected, which lets us create a
custom error.
▻ The catch statement allows you to define a block of code to be executed if an error occurs in the
try block. 4

S.Muralidharan 2
Dept of EEE Object Oriented Programming

try
{
// some code that may throw an exception
}
Catch(exception &e)
{
// some processing to attempt to recover from error
// based on information carried by the exception
}

S.Muralidharan 3
Dept of EEE Object Oriented Programming

SIMPLE EXAMPLE
#include <iostream>
using namespace std;

int main()
{
int x = -1;
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0) Output:
{ Before try
throw x; Inside try
cout << "After throw (Never executed) \n"; Exception Caught
} After catch (Will be executed)
}
catch (int x ) {
cout << "Exception Caught \n";
}

cout << "After catch (Will be executed) \n";


return 0;
}

Default Exception

#include <iostream>
using namespace std;

int main()
{
try {
throw 10; Output:
} Default Exception
catch (char e) {
cout << "Caught " << e;
}
catch (...) {
cout << "Default Exception\n";
}
return 0;
}

S.Muralidharan 4
Dept of EEE Object Oriented Programming

Nested handling & Rethrow

#include <iostream>
using namespace std;

int main()
{
try {
try {
throw 20;
}
catch (int n) { Output:
cout << "Handle Partially "; Handle Partially Handle remaining
throw; // Re-throwing an exception
}
}
catch (int n) {
cout << "Handle remaining ";
}
return 0;
}

▰ When an exception is thrown, all objects created inside the enclosing try block are destroyed
before the control is transferred to the catch block.
#include <iostream>
using namespace std;

class Test {
public:
Test() { cout << "Constructor of Test " << endl; }
~Test() { cout << "Destructor of Test " << endl; } Output:
}; Constructor of Test
int main() Destructor of Test
{ Caught 10
try {
Test t1;
throw 10;
}
catch (int i) {
cout << "Caught " << i << endl;
}
} 10

S.Muralidharan 5
Dept of EEE Object Oriented Programming

▰ If none of the catch handlers for a try block matches a thrown exception the exception
moves to the next enclosing try block.

▰ If there is no match in any enclosing try block the exception is uncaught. An uncaught
exception also occurs if a new exception is thrown before an existing one is handled.
Cleanups may fail to occur with an uncaught exception, so this is an error.

▰ If an exception is uncaught the special function terminate() is called.

▰ Uncaught exceptions can always be avoided by enclosing the contents of main in a try block
with an ellipsis handler.

11

C++ specifying exceptions

▰ It is possible to restrict a function to throw only certain specified exceptions. This is achieve
by adding a keywords throw list clause to the function definition. The general form for using
an exception specification is in below example
type function(arg-list) throw (type list)
{
………
………
………
function body
}
▰ The type list specifies the type of exceptions that may be thrown show in the above
program. if Throwing any other type of exceptions in program it will cause abnormal
program termination. if we wish to prevent a function from throwing any exception, we may
do by making the type-list empty inside arguments. That is we must use
throw(); // Empty list 12

S.Muralidharan 6
Dept of EEE Object Oriented Programming

void main()
{
try
{
#include < iostream > cout << “Testing throw restrictions \n”;
using namespace std; cout << “X==0 \n”;
void test(int x) throw(int,double) test(0);
{ cout << “x==1 \n”;
if(x==0) throw ‘x’; // char test(1);
cout << “x==-1 \n; Output:
else
test(-1); Testing Throw RESTRICTIONS
if(x==1) throw; // int
cout << “x==2 \n”; x==0
else
test(2); caught a character
if(x==-1) throw 1.0; // double
} End of try catch system
cout<<“End of the function block \n”;
} catch(char c)
{
cout << “caught a character \n”;
}
catch(int m)
{
cout << “caught an integer \n”;
}
catch(double d)
{
cout << “caught a double \n”;
} 13
cout << “End of try catch system \n\n”;
}

S.Muralidharan 7

You might also like