Exception handling
Exception handling
SDF II(15B11CI211)
EVEN Semester
Contents
• Types of Errors
• Introduction to Exceptions, Try, Catch and Throw
• Re-throwing exceptions, Exception and Inheritance
• Case Study of Exceptions
4
Types of Errors
• Errors can be broadly categorized into two types.
• Compile Time Errors
• 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.
5
• The process of converting system error messages into user friendly error message
is known as Exception handling.
• This is one of the powerful feature of C++ to handle run time error and maintain
normal flow of C++ application.
• Exception : An exception is an event, which occurs during the execution of a
program, that disrupts the normal flow of the program's Instructions.
7
• try: Try block consists of the code that may generate exception. Exception are
thrown from inside the try block.
• try: represents a block of code that can throw an exception.
• "try" block groups one or more program statements with one or more catch
clauses.
11
• catch: Catch block catches the exception thrown by throw statement from try
block. Then, exception are handled inside catch block.
• catch :The catch block defines the action to be taken, when an exception occur.
13
• Multiple catch exception statements are used when a user wants to handle
different exceptions differently.
• For this, a user must include catch statements with different declaration.
16
17
• Sometimes, it may not be possible to design a separate catch block for each kind
of exception. In such cases, we can use a single catch statement that catches all
kinds of exceptions.
18
• Sometimes, it may not be possible to design a separate catch block for each kind
of exception. In such cases, we can use a single catch statement that catches all
kinds of exceptions.
19
Catch block
• Data-type specifies the type of exception that catch block will handle, Catch block will
receive value, send by throw keyword in try block.
Multiple Catch Blocks 21
#include <iostream>
int main() {
int a=2;
try {
cout<<"\nEnd of program."; }
22
23
Example Continued
27
Exercise
Rethrowing 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.
• C++ provides a range of built in exceptions. The base class for all exceptions classes is
exception
33
• In the previous program even though the exception thrown is of the type Derived
it is caught by the catch block of the type Base.
• To avoid that we have to write the catch block of Base type at last in the sequence
Example 42
#include <iostream>
using namespace std;
class Base {};
class Derived : public Base {};
int main() {
try {
throw Derived(); }
catch(Derived d) {
cout<<"Derived object caught"; }
catch(Base b) {
cout<<"Base object caught"; }
return 0; }
43
44
References
• https://www.slideshare.net/AdilAslam4/exception-handling-in-c-69353237
• https://www.tutorialspoint.com/cplusplus/pdf/cpp_exceptions_handling.pdf
• C, the complete reference Book by Herbert Schildt