0% found this document useful (0 votes)
10 views

Lect 16 - exception handling

It is a ppt which gives idea about file handling in OOP C++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lect 16 - exception handling

It is a ppt which gives idea about file handling in OOP C++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

OBJECT ORIENTED PROGRAMMING

INSTRUCTOR: Ms. Sanneya Aziz


EXCEPTION HANDLING
o An exception is a problem that arises during the execution of a program.
o 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.
o Exceptions provide a way to transfer control from one part of a
program to another.
o C++ exception handling is built upon three keywords: try,
catch, and throw.
EXCEPTION HANDLING

 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.
WHY EXCEPTION HANDLING

 Separation of Error Handling code from Normal Code.


 Functions/Methods can handle any exceptions they choose.
 Grouping of Error Types
EXCEPTION HANDLING
 Code within a try/catch block is catch( ExceptionName e2 )
referred to as protected code. { // catch block }
The syntax for using try/catch as catch( ExceptionName eN )
follows
{ // catch block }
try {
// protected code
}
catch( ExceptionName e1 )
{ // catch block }
THROWING EXCEPTIONS

 Exceptions can be thrown anywhere within a code block


using throw statement.
 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);
}
EXAMPLE
//which throws a division by zero int y = 0;
exception and we catch it in catch block. double z = 0;
#include <iostream> try {
using namespace std; z = division(x, y);
double division(int a, int b) cout << z << endl;
{ if( b == 0 ) }
{ catch (const char* msg)
throw "Division by zero condition!"; {
} cerr << msg << endl;
return (a/b); } }
int main () return 0; }
{ int x = 50;
output :
Before try
Inside try
EXCEPTION HANDLING EXAMPLE Exception Caught
After catch (Will be executed)

#include <iostream> cout << "After throw (Never


using namespace std; executed) \n"; }
int main() }
{ catch (int x ) {
int x = -1; cout << "Exception Caught \n";
cout << "Before try \n"; }
try { cout << "After catch (Will be
cout << "Inside try \n"; executed) \n";
if (x < 0) return 0;
{ throw x; }
C++ STANDARD EXCEPTIONS

 C++ provides a list of standard exceptions defined


in <exception> which we can use in our programs. These are arranged
in a parent-child class hierarchy shown in next slide.
Sr.# Exception & Description
std::exception
1
An exception and parent class of all the standard C++ exceptions.
std::bad_alloc
2
This can be thrown by new.
std::bad_cast
3
This can be thrown by dynamic_cast.
std::bad_exception
4
This is useful device to handle unexpected exceptions in a C++ program.
std::bad_typeid
5
This can be thrown by typeid.
std::logic_error
6
An exception that theoretically can be detected by reading the code.
std::domain_error
7
This is an exception thrown when a mathematically invalid domain is used.
Sr.No Exception & Description
std::invalid_argument
8
This is thrown due to invalid arguments.
std::length_error
9
This is thrown when a too big std::string is created.
std::out_of_range
10 This can be thrown by the 'at' method, for example a std::vector and
std::bitset<>::operator[]().
std::runtime_error
11
An exception that theoretically cannot be detected by reading the code.
std::overflow_error
12
This is thrown if a mathematical overflow occurs.
std::range_error
13
This is occurred when you try to store a value which is out of range.
std::underflow_error
14
This is thrown if a mathematical underflow occurs.
THINK

 Can there be try-catch block nested?


 If an exception is thrown and not caught anywhere what happens ?
ANSWER

 Can there be try-catch block nested?

In C++, try-catch blocks can be nested. Also, an exception can be re-


thrown using “throw; ”
output :
Handle partially
EXAMPLE Handle remaining

#include <iostream> cout << "Handle Partially ";


using namespace std; throw 10; //Re-throwing an
int main() exception
{ }
try { }
try { catch (int n) {
throw 20; cout << "Handle remaining ";
} }
catch (int n) return 0;
{ }
ANSWER

 If an exception is thrown and not caught anywhere what happens ?

If an exception is thrown and not caught anywhere, the program


terminates abnormally. For example, in the following program, a char is
thrown, but there is no catch block to catch a char.
EXAMPLE
#include <iostream>
using namespace std;
int main()
{ output :
terminate called after
try { throwing an instance of
throw 'a'; 'char' This application has
requested the Runtime to
} terminate it in an unusual
way. Please contact the
catch (int x) { application's support team
cout << "Caught "; for more information.
}
return 0; }

You might also like