Clas-PPT-C++-Exception Handling
Clas-PPT-C++-Exception Handling
What is exception?
• Exception in a C++ programs refers an unexpected circumstances like
runtime errors.
• To catch the exceptions, you place some section of code under exception
inspection. The section of code is placed within the try-catch block.
• You will separate your error handling code from your normal
code.
int main () {
int m = 50;
int n;
double p = 0;
try {
cin>>n;
p = divisionz(m, n);
cout << p << endl;
}
catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Example of Exception Handling
• Include the iostream header file in the program to use its functions.
• Include the std namespace in the program to its classes without calling it.
• Create a function named division that takes two integer arguments, x, and y. The function should
return a double result.
• Use an if statement to check whether the value of variable argument y is 0. The { marks the
beginning of if body.
• The message to be returned/thrown if y is 0.
• End of the body of the if statement.
• The zeroDivision function should return the value of x/y.
• End of the body of the zeroDivision function.
• Call the main() method. The { marks the beginning of this method.
• Declare an integer variable and assigning it the value 11.
• Declare an integer variable b and assigning it the value 0.
• Declare a double variable c and assigning it the value 0.
• Use the try statement to catch an exception. The { marks the beginning of the body of try/catch
block. The code added within the body will become the protected code.
• Call the zeroDivision function and passing to arguments a and b, that is, 11 and 0. The result of this
operation will be stored in variable c.
• Print out the value of variable c on the console.
• End of the body of try/catch block.
• Catch the exception. The returned error message will be stored in the variable message.
• Print out the returned error message on the console.
• End of the body of the catch block.
• The program should return a value upon successful execution.
• End of the main() function body.
#included <iostream.h>
using namespace std;
void fun1(){
//body of the function
throw exception1;
}
int main(){
try{
fun1();
//body not want to print if the exception occur
}
catch(//type1){
//body to handle the exception
}
catch(//type2){
//body to handle the exception
}
catch(//type3){
//body to handle the exception
}
catch(//type4){
//body to handle the exception
}
chtch(…){
}
return 0;
}
Rethrowing
• An exception handler can process an exception or can not process that.
• In that case the exception handler refer the exception to another
exception handler.
• This can be achieved by rethrowing the exception.
• That can be done by the statement
– throw;
• The rethrown exception is detected by next enclosing try block.
• When an exception is rethrown that can be handled by the catch listed
after the try block.
• If an empty throw statement is there outside of a catch handler the
functions terminate. Then abandons exception processing terminates the
program immediately.
struct E { try{
const char* message; cout<<"In the try outside the main"<<endl;
E() : message("Class E") { } throw;
}; }
catch (...) {
struct E1 : E { cout << "In handler of catch (...) outside main in catch
const char* message; 2nd" << endl;
E1() : message("Class E1") { } }
};
}
// Initialization of class
class Test {
public:
// Constructor of class
Test()
{
cout << "Constructing an object of class Test "<< endl;
}
// Destructor of class
~Test()
{
cout << "Destructing the object of class Test "<< endl;
}
};
int main()
{
try {
// Calling the constructor
Test t1;
throw 10;
class Test {
static int count; // Used static to initialise the scope
// Of 'count' till lifetime
int id;
public:
// Constructor
Test()
{
count++;
id = count;
cout << "Constructing object number " << id << endl;
if (id == 4)
throw 4;
}
// Destructor
~Test()
{
cout << "Destructing object number " << id << endl;
}
};
int Test::count = 0;
// Source code
int main()
{
try {
Test array[5];
}
catch (int i) {
cout << "Caught " << i << endl;
}
}
Exception Specification
• If a throw statement has a list of exception types that means the function
can throw any one type of exception.
• Example:
int g( double h ) throw (Exception1, Exception2, Exception3)
{
// function body
}
• If the function throw an exception that does not match with the type then
function unexpected will be called and the program will be terminated.
• If a function doesn’t have any exception then it can throw any exception.
(syntax: throw())
• Some compiler ignore exception specification.
Exception & Inheritance
• Different exception class can be derived from common base class.
• In C++ standard library there is a base class exception which has a virtual
function what that will help us to return error message (returns the error
message of an exception object ).
• Method:
– new returns 0, abort, does not allow program to recover
– new throws bad_alloc exception
– use new(nothrow) instead of new to have new return 0 when it fails.
• new(notthor)
– Function set_new_handler(functionName) sets which function is
called when new fails.
– Function can return no value and take no arguments
– new will not throw bad_alloc
Exception Hierarchy
Standard Exception
Exception Description
std::runtime_error This is an exception that cannot be detected via reading the code.
int main() {
try {
throw newex;
}
catch (exception& ex) {
cout << ex.what() << '\n';
}
return 0;
}
Explanation
• Include the iostream header file in our program. We will use its functions without getting errors.
• Include the exception header file in our program. We will use its functions like what without errors.
• Include the std namespace in our program to use its classes without calling it.
• Create a new class named newException. This class inherits the exception class of C++.
• The beginning of the class body.
• Overwrite the virtual member function what() defined in the exception header file. We will then
describe our own exception, the new exception.
• Start the definition of the new exception.
• The message to be returned if the new exception is caught.
• End of the definition of the new exception.
• End of the body of class newException. The newex is the name to be used to catch our new
exception, after which the newException will be called.
• Call the main() function. The program logic should be added within its body. The { marks the
beginning of its body.
• Use a try statement to mark the code within which we need to mark the exception. The { marks the
beginning of the body of try/catch block. The code surrounded by this will become protected.
• Throw the newex exception if it’s caught.
• End of the try body.
• Use the catch statement to catch the exception. The exception error message will be stored in
variable ex.
• Print the exception error message on the console.
• End of the body of catch statement.
• The program should return a value if it executes successfully.
• End of the body of the main() function.
Thank You