EXPERIMENT NO 10 Exceptions in C++ Objective: To learn handling of exceptions in C++ Equipment Required Visual Studio/Dev C++ Exception Handling: Errors can be broadly categorized into two types. We will discuss them one by one. 1. Compile Time Errors 2. Run Time Errors Compile Time Errors – Errors caught during compiled time is called Compile time errors. Compile time errors include library reference, syntax error or incorrect class import. Run Time Errors - They are also known as exceptions. An exception caught during run time creates serious issues. Errors hinder normal execution of program. Exception handling is the process of handling errors and exceptions in such a way that they do not hinder normal execution of the system. For example, User divides a number by zero, this will compile successfully but an exception or run time error will occur due to which our applications will be crashed. In order to avoid this we'll introduce exception handling technics in our code. In C++, Error handling is done using three keywords: • try • catch • throw Syntax: try { //code throw parameter; } catch(exceptionname ex) { //code to handle exception } try block The code which can throw any exception is kept inside(or enclosed in) a try block. Then, when the code will lead to any error, that error/exception will get caught inside the catch block. catch block catch block is intended to catch the error and handle the exception condition. We can have multiple catch blocks to handle different types of exception and perform different actions when the exceptions occur. For example, we can display descriptive messages to explain why any particular excpetion occured. throw statement It is used to throw exceptions to exception handler i.e. it is used to communicate information about error. A throw expression accepts one parameter and that parameter is passed to handler. throw statement is used when we explicitly want an exception to occur, then we can use throw statement to throw or generate that exception. set_terminate( ) #include <exception> #include <iostream> #include <cstdlib> using namespace std; void terminator() { cout << "I'll be back!" << endl; exit(0); } void (*old_terminate)() = set_terminate(terminator); class Botch { public: class Fruit {}; void f() { cout << "Botch::f()" << endl; throw Fruit(); } ~Botch() { throw 'c'; } }; int main() { try{ Botch b; b.f(); } catch(...) { cout << "inside catch(...)" << endl; } } ///:~ You can install your own terminate( ) function using the standard set_terminate( ) function, which returns a pointer to the terminate( ) function you are replacing, so you can restore it later if you want. Your custom terminate( ) must take no arguments and have a void return value. In addition, any terminate( ) handler you install must not return or throw an exception, but instead must call some sort of program-termination function. If terminate( ) is called, it means the problem is unrecoverable. Like unexpected( ), the terminate( ) function pointer should never be null. old_terminate looks a bit confusing at first: It not only creates a pointer to a function, but it initializes that pointer to the return value of set_terminate( ). Even though you may be familiar with seeing a semicolon right after a pointer-to-function definition, it’s just another kind of variable and may be initialized when it is defined. The class Botch not only throws an exception inside f( ), but also in its destructor. This is one of the situations that causes a call to terminate( ), as you can see in main( ). Even though the exception handler says catch(...), which would seem to catch everything and leave no cause for terminate( ) to be called, terminate( ) is called anyway, because in the process of cleaning up the objects on the stack to handle one exception, the Botch destructor is called, and that generates a second exception, forcing a call to terminate( ). Thus, a destructor that throws an exception or causes one to be thrown is a design error. LAB TASKS: 1. Write a C++ code and use exception handling to handle a division by zero. 2. Write a C++ code in which if a value in array is greater than 2 int exception is thrown else char exception is thrown 3. Implement and run the set_terminate() example above and see how it works?