Sunteți pe pagina 1din 18

MODULE -4

TOPIC: EXCEPTION HANDLING

1
MODULE -4

OUTLINE
 Definition

 Syntax

 Throwing the exception

 Catching the exception

 Example

Module 4 Session 2 2
MODULE -4

DEFINITION
• An exception is a problem that arises during the execution of a
program.
 Exceptions are events that occur during the execution of
programs that disrupt the normal flow of instructions (e.g.
divide by zero, array access out of bound, etc.).
 Exception objects can be thrown and caught.

Module 4 Session 2 3
Module 4 Session 2
Module 4 Session 2 4
MODULE -4

EXCEPTION HANDLING TERMS


C++ exception handling is built upon three keywords: try, catch, and
throw.
try − A try block identifies a block of code for which particular
exceptions will be activated. It's followed by one or more catch blocks
throw − A program throws an exception when a problem shows up.
This is done using a throw keyword.
catch − A program catches an exception with an exception handler at
the place in a program where you want to handle the problem. The
catch keyword indicates the catching of an exception.
.

Module 4 Session 2 5
MODULE -4

SYNTAX
try
{ // protected code }
catch( ExceptionName e1 )
{ // catch block }
catch( ExceptionName e2 )
{ // catch block }
catch( ExceptionName eN )
{ // catch block }

Module 4 Session 2 6
MODULE -4

THROWING EXCEPTIONS
Exceptions can be thrown anywhere within a code block using
throw statement. The operand of the throw statement determines a
type for the exception and can be any expression and the type of
the result of the expression determines the type of exception
thrown.
Following is an example of throwing an exception when dividing by
zero condition occurs

double division(int a, int b)


{
if( b == 0 )
{ throw "Division by zero condition!"; }
return (a/b);
}

Module 4 Session 2 7
MODULE -4

CATCHING EXCEPTIONS

The catch block following the try block catches any exception.
We can specify what type of exception you want to catch and this is
determined by the exception declaration that appears in
parentheses following the keyword catch.

try
{ // protected code }
catch( ExceptionName e )
{ // code to handle ExceptionName exception }

Module 4 Session 2 8
MODULE -4

CATCHING EXCEPTIONS…

If you want to specify that a catch block should handle any type of
exception that is thrown in a try block, you must put an ellipsis, ...,
between the parentheses enclosing the exception declaration as
follows −

try
{ // protected code }
catch(...)
{ // code to handle any exception }

Module 4 Session 2 9
# MODULE -4

include <iostream>
EXAMPLE
using namespace std;
catch (int x ) {
int main() cout << "Exception Caught \n";
{ }
int x = -1;
cout << "After catch (Will be
// Some code executed) \n";
cout << "Before try \n"; return 0;
try { }
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never
executed) \n";
}
}

Module 4 Session 2 10
MODULE -4

EXAMPLE
#include <iostream>
using namespace std;
int main()
{
try {
throw 'a';
}
catch (int x) {
cout << "Caught " << x;
}
catch (...) {
cout << "Default Exception\n";
}
return 0;
}
Module 4 Session 2 11
MODULE -4

EXAMPLE
#include <iostream> int main ()
using namespace std; {
int x = 50; int y = 0;
double division(int a, int b) double z = 0;
{ try
{
if( b == 0 ) z = division(x, y);
{ cout << z << endl;
}
throw "Division by zero catch (const char* msg)
condition!"; { cerr << msg << endl;
}
} return (a/b); return 0;
} }

Module 4 Session 2 12
MODULE -4

#include <iostream> CATCHING EXCEPTIONS…


#include <cstring>
using namespace std;

class MyException
{
public:
char str_what[80];
int what;
MyException(char *s, int e) {
strcpy(str_what, s);
what = e;
}
};

Module 4 Session 2 13
MODULE -4

int main() CATCHING EXCEPTIONS…


{
int i;
try {
cout << "Enter a positive Enter a positive number: -4
number: "; Not Positive: -4
cin >> i;
if(i<0)
throw MyException("Not
Positive", i);
}
catch (MyException e) { // catch
an error
cout << e.str_what << ": ";
cout << e.what << "\n";
}
return 0; 14
Module 4 Session 2
MODULE -4

CATCHING EXCEPTIONS…
#include <iostream>
using namespace std;
void Xhandler(int test)
{
try{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
catch(int i) { // catch an int exception
cout << "Caught an integer\n";
}
catch(...) { // catch all other exceptions
cout << "Caught One!\n";
}
}

Module 4 Session 2 15
MODULE -4

CATCHING EXCEPTIONS…
int main()
{
cout << "Start\n";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "End";
return 0;
}
The output produced by this program is
shown here.
Start
Caught an integer
Caught One!
Caught One!
End

Module 4 Session 2 16
MODULE -4

CATCHING EXCEPTIONS…
Rethrowing an Exception

#include <iostream>
using namespace std;
void Xhandler()
{
try {
throw "hello"; // throw a char *
}
catch(const char *) { // catch a char *
cout << "Caught char * inside Xhandler\n";
throw ; // rethrow char * out of function
}
}

Module 4 Session 2 17
MODULE -4

CATCHING EXCEPTIONS…
int main()
{
cout << "Start\n"; This program displays this output:
try{ Start
Caught char * inside Xhandler
Xhandler(); Caught char * inside main
} End

catch(const char *)
{
cout << "Caught char * inside main\n";
}
cout << "End";
return 0;
}
Module 4 Session 2 18

S-ar putea să vă placă și