Transcript Slide 1

Exception handling
Object Oriented Methods
Presented by:
Yassine BHIJA
Akhtar AHMED SYED
Haleem MOHAMMED ABDUL
Introduction
Object Oriented Method is
a powerful way to approach the job of the
programming.
Object oriented programming
took the best ideas of structured programming
and combined them with several new
concepts.
Introduction

Object Oriented Programs are organized
around the principle 'data controlling
access to code'.

Object Oriented Programming languages
have three traits in common

Encapsulation

Polymorphism

Inheritance.
C++
 C++
began as an expanded version of C.
 The
Increasing complexity necessitated
the invention of C++.
 The
essence of C++ is to allow the
programmer to comprehend and manage
larger and more complex programs.
Exceptions and exceptions
handling
 During
the normal flow of execution of a
programme there could be a rise of
condition which hinders the normal flow.
This generated condition is called an
exception.
 Exceptions
handling is to raise a flag every
time something goes wrong
How it works

When an exception occurs it creates an
object which is called as exceptional object.
 These
objects contain the information
about the error which includes the type and
state of the exception
How it works

This exceptional objects hands the normal
flow to the exceptional handler

This portion of exceptional codes is in the
form of stack
How it works
The exceptional codes are executed to
handle that particular exception.

The subroutine returns the flow of execution
to the original location by using the saved
information to restore.

Catch the exception
 Sometimes
it happens when the
exceptional handler cannot match with the
exception from the stack, then this
exceptional handler chosen is referred as
“catch the exception”

In that case, the system or the main
programme gets terminated.
Exception handling in C

An example in C for handling division by
zero is as follows:
double inv(double x)
{
if (x != 0) return 1.0/x;
else
{
throw_exception("Division by zero");
return 0;
}
}
Exception handling in JAVA
 In
java which is an another object oriented
language the exceptional handling is done
like the example below:
try
{
… normal program code
}
catch(Exception e)
{
… exception handling code
}
Advantages of E.H
 A programme
can be written without an
error code and later added.
It
is possible to create groups of exceptions
and handle them in a general way
Classical exceptions
Any C++ programmer might have faced
some of these exceptions:
 The
open error
 A size error
 The conversion exception
 The division by zero
The semantic, the syntax
To write an exception on C++ there is a
special syntax; three main semantics are more
often used:
 try...finally
 try...except
 try...throw...catch
It is noticed that try is a keyword which
introduces the exception, it identifies a block of
code in which an exception can occur.
try...finally
Using finally is to guarantee that a block will
be executed whether there is an exception or
not:
code;
try
// the block protected
code_protected;
finally
// this code is executed either at the end of the one before
// or just after an exception
code_that_should_be_executed;
end;
code;
try...except
The except block is used if and only if an
exception occurs on the try block
try
// the block protected
code_protected;
except
// this code is executed if and only if
// an exception arise in the block before
// it is possible to handle exceptions depending on their classes
// so for instance it will be
on Exception1 do code_For_Exception1;
on Exception2 do code_For_Exception2;
....
on Exception(n) do code_For_Exception(n);
else
code_For_Other_Exceptions;
end;
try...throw...catch
The most used semantics is this one and it is
used in the following way :
try{
if(condition1)
throw string("here is a string");
else if(condition2)
throw 5;
}
catch(const string&)
// a program should be executed if the string is thrown
try...throw...catch
The idea here is " (trying) something, (throwing) an exception,
and subsequently (catching) it" (Stephens et al., 2005)
The keyword catch has the ability to catch all the exceptions:



catch (TYPE): intercept the exceptions of the TYPE type, as well as
those of its derived classes.
catch(TYPE e): It is the same thing that before but here, the object
"e" is used to get some more information about the nature of the
exception.
catch ( ... ) : intercept all the types of exceptions with the precedent
catch blocks
Use of standard library

Exist in the STL a class for exceptions, it can be
used by including the header <exception> under
the namespace std

It is in good programming practice to derivate all
the exceptions from this header

Sometimes it is the header <stdexcept> which is
included, it should be known that itself includes
the header <exception>.
Conclusion
To sum up:

Exceptions handling is to raise a flag every time something
goes wrong
Mainly
there is three structures used for exception handling
in C++
Try... finally
Try... except
Try... throw... catch
The
STL contains exception class under the header
<exception>, and it is in good programming practice to
derivate all the exceptions from this class