 An exception is a situation which is unusual 
for a program that is being processed 
What is Exception Handling ? 
The ability to deal with a program’s eventual 
abnormal behavior is called Exception handling.
C++ provides three keywords to 
1. Try (Trying the normal flow) 
2. Catch (Catching Errors) 
3. Throw (Throwing an error) 
handle an exception.
Syntax: try {Behavior} 
Try (Trying the normal flow) 
It lets the compiler know that you are 
anticipating an abnormal behavior and will try 
to deal with it.
Syntax: catch(Argument) {WhatToDo} 
Catch (Catching Errors) 
During the flow of the program as part of the 
try section 
 if an abnormal behavior occurs, instead of 
letting the program crash 
you can transfer the flow of the program to 
another section that can deal with it and that 
section is Catch Section.
try { 
CCoommbbiinneedd wwiitthh tthhee ttrryy bblloocckk,, 
tthhee ssyynnttaaxx ooff aann eexxcceeppttiioonn wwoouulldd bbee:: 
// Try the program flow 
} 
catch(Argument) 
{ 
// Catch the exception 
}
We can also chain multiple handlers (catch 
expressions), each one with a different 
parameter type 
try { 
// code here 
} 
catch (int param) 
{ cout << "int exception"; 
} 
catch (char param) 
{ cout << "char exception"; 
} 
catch (...) 
{ cout << "default exception"; 
}
The transfer of control from the try block to 
the catch clause is carried by the throw 
keyword. 
Eg : 
throw ; 
throw "Positive Number Required"; 
TThhrrooww ((TThhrroowwiinngg aann eerrrroorr))
NNooww 
LLeett''ss FFaaccee 
SSoommee EExxcceeppttiioonnss......
#include <iostream> 
int main() 
{ 
double Number1, Number2, Result; 
// Request two numbers from the user 
cout << "Please provide two numbersn"; 
try { 
cout << "First Number: "; 
cin >> Number1; 
cout << "Second Number: "; 
cin >> Number2;
if( Number2 == 0 ) 
throw"Cant divide by zero"; 
// Perform a division and display the result 
Result = Number1 / Number2; 
cout << "n" << Number1 << " / " << Number2 << " = " << Result 
<< "nn"; 
} 
catch(const char* Message) 
{ cout << "Error: " << Message; 
} 
getch(); 
return 0; 
}
WWhheenn YYoouu FFaaccee AAnn EExxcceeppttiioonn
NNooww 
LLeett''ss FFaaccee 
AAnnootthheerr EExxcceeppttiioonn......
#include<conio.h> 
#include<iostream.h> 
class factorial 
{ int i,fact; 
public: 
void facto(int n) 
{ fact=1; 
for(i=1;i<=n;i++) 
{ fact=fact*i; 
} 
cout<<"Factorial of a given no. is"<<fact; 
} 
};
int main() 
{ clrscr(); 
int n; 
factorial f; 
cout<<"Enter no.n"; 
cin>>n; 
try 
{ if(n<0) 
throw"Please Enter natural no !!!"; 
f.facto(n); 
} 
catch(const char *message) 
{ cout<<"Error:"<<message; 
} 
getch(); 
return 0; 
}
WWhheenn YYoouu FFaaccee AAnn EExxcceeppttiioonn
QQuuiizz TTiimmee……
Q1. Identify tthhee ccoorrrreecctt ssttaatteemmeenntt ?? 
1. There must not be any code between the try’s 
closing bracket and the catch section. 
2. There can be any code between the try’s closing 
bracket and the catch section. 
3. There may be any code between the try’s closing 
bracket and the catch section. 
4. There must be any code between the try’s closing 
bracket and the catch section.
QQ22.. TThheerree ccaann bbee mmuullttiippllee ccaattcchh 
bblloocckk iinn pprrooggrraamm ?? 
1. True 
2. False
QQ33.. WWhhiicchh ooff tthheessee llaanngguuaaggee ddooeess nnoott 
ssuuppppoorrtt EExxcceeppttiioonn hhaannddlliinngg ?? 
1. Java 
2. C++ 
3. C
Q4. Which of tthheessee ssttaatteemmeennttss iiss ccoorrrreecctt ?? 
1. Try 3. Try 
{ throw; { throw 
}; }; 
2. Try 4. Try 
{ throw; { throw 
} }
Q5. WWhhiicchh ooff tthheessee ssttaatteemmeennttss iiss nnoott 
ccoorrrreecctt ?? 
1. Catch(….){} 
2. Catch(…){} 
3. Catch(const char *message){} 
4. Catch(int message){}
Exception handling

Exception handling

  • 2.
     An exceptionis a situation which is unusual for a program that is being processed What is Exception Handling ? The ability to deal with a program’s eventual abnormal behavior is called Exception handling.
  • 3.
    C++ provides threekeywords to 1. Try (Trying the normal flow) 2. Catch (Catching Errors) 3. Throw (Throwing an error) handle an exception.
  • 4.
    Syntax: try {Behavior} Try (Trying the normal flow) It lets the compiler know that you are anticipating an abnormal behavior and will try to deal with it.
  • 5.
    Syntax: catch(Argument) {WhatToDo} Catch (Catching Errors) During the flow of the program as part of the try section  if an abnormal behavior occurs, instead of letting the program crash you can transfer the flow of the program to another section that can deal with it and that section is Catch Section.
  • 6.
    try { CCoommbbiinneeddwwiitthh tthhee ttrryy bblloocckk,, tthhee ssyynnttaaxx ooff aann eexxcceeppttiioonn wwoouulldd bbee:: // Try the program flow } catch(Argument) { // Catch the exception }
  • 7.
    We can alsochain multiple handlers (catch expressions), each one with a different parameter type try { // code here } catch (int param) { cout << "int exception"; } catch (char param) { cout << "char exception"; } catch (...) { cout << "default exception"; }
  • 8.
    The transfer ofcontrol from the try block to the catch clause is carried by the throw keyword. Eg : throw ; throw "Positive Number Required"; TThhrrooww ((TThhrroowwiinngg aann eerrrroorr))
  • 9.
    NNooww LLeett''ss FFaaccee SSoommee EExxcceeppttiioonnss......
  • 10.
    #include <iostream> intmain() { double Number1, Number2, Result; // Request two numbers from the user cout << "Please provide two numbersn"; try { cout << "First Number: "; cin >> Number1; cout << "Second Number: "; cin >> Number2;
  • 11.
    if( Number2 ==0 ) throw"Cant divide by zero"; // Perform a division and display the result Result = Number1 / Number2; cout << "n" << Number1 << " / " << Number2 << " = " << Result << "nn"; } catch(const char* Message) { cout << "Error: " << Message; } getch(); return 0; }
  • 12.
    WWhheenn YYoouu FFaacceeAAnn EExxcceeppttiioonn
  • 13.
    NNooww LLeett''ss FFaaccee AAnnootthheerr EExxcceeppttiioonn......
  • 14.
    #include<conio.h> #include<iostream.h> classfactorial { int i,fact; public: void facto(int n) { fact=1; for(i=1;i<=n;i++) { fact=fact*i; } cout<<"Factorial of a given no. is"<<fact; } };
  • 15.
    int main() {clrscr(); int n; factorial f; cout<<"Enter no.n"; cin>>n; try { if(n<0) throw"Please Enter natural no !!!"; f.facto(n); } catch(const char *message) { cout<<"Error:"<<message; } getch(); return 0; }
  • 16.
    WWhheenn YYoouu FFaacceeAAnn EExxcceeppttiioonn
  • 17.
  • 18.
    Q1. Identify tthheeccoorrrreecctt ssttaatteemmeenntt ?? 1. There must not be any code between the try’s closing bracket and the catch section. 2. There can be any code between the try’s closing bracket and the catch section. 3. There may be any code between the try’s closing bracket and the catch section. 4. There must be any code between the try’s closing bracket and the catch section.
  • 19.
    QQ22.. TThheerree ccaannbbee mmuullttiippllee ccaattcchh bblloocckk iinn pprrooggrraamm ?? 1. True 2. False
  • 20.
    QQ33.. WWhhiicchh oofftthheessee llaanngguuaaggee ddooeess nnoott ssuuppppoorrtt EExxcceeppttiioonn hhaannddlliinngg ?? 1. Java 2. C++ 3. C
  • 21.
    Q4. Which oftthheessee ssttaatteemmeennttss iiss ccoorrrreecctt ?? 1. Try 3. Try { throw; { throw }; }; 2. Try 4. Try { throw; { throw } }
  • 22.
    Q5. WWhhiicchh oofftthheessee ssttaatteemmeennttss iiss nnoott ccoorrrreecctt ?? 1. Catch(….){} 2. Catch(…){} 3. Catch(const char *message){} 4. Catch(int message){}