SlideShare a Scribd company logo
Exception handling
 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

More Related Content

PPTX
Exception handling c++
PPTX
C++ ala
PDF
Exception handling
PPT
Exception handling and templates
PPTX
Exception Handling in C++
PPT
Exceptions in c++
PDF
Exception Handling in the C++ Constructor
PPTX
exception handling in cpp
Exception handling c++
C++ ala
Exception handling
Exception handling and templates
Exception Handling in C++
Exceptions in c++
Exception Handling in the C++ Constructor
exception handling in cpp

What's hot (20)

PPTX
Exception Handling in object oriented programming using C++
PPTX
Exception handling chapter15
PPT
Exception handling
PDF
14 exception handling
PPT
Handling Exceptions In C &amp; C++[Part A]
PPTX
What is Exception Handling?
PPSX
Exception Handling
PPTX
Exception handling
PPT
Exception handling
PPTX
Exception handling in java
PPT
Week7 exception handling
PPTX
130410107010 exception handling
PPT
Unit iii
PDF
EXCEPTION HANDLING in C++
PPT
Exception Handling in JAVA
PPS
Exception handling in c programming
PDF
Exception handling
PPT
Exception handling in java
PPTX
Exception handling
PPTX
Exception handling in c++
Exception Handling in object oriented programming using C++
Exception handling chapter15
Exception handling
14 exception handling
Handling Exceptions In C &amp; C++[Part A]
What is Exception Handling?
Exception Handling
Exception handling
Exception handling
Exception handling in java
Week7 exception handling
130410107010 exception handling
Unit iii
EXCEPTION HANDLING in C++
Exception Handling in JAVA
Exception handling in c programming
Exception handling
Exception handling in java
Exception handling
Exception handling in c++
Ad

Similar to Exception handling (20)

PPT
Exceptions in C++exception handling in C++, computer programming.ppt
PPTX
Object Oriented Programming Using C++: C++ Exception Handling.pptx
PDF
Exceptions and Exception Handling in C++
PPT
Lecture16
PPTX
15_exception_handling-1-exception handling .pptx
PPT
Exception_Handling_in_C__1701342048430.ppt
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
PPTX
Unit II Java & J2EE regarding Java application development
PPT
exceptiondddddddddddddddddddddddddddddddddddddddd.ppt
PPTX
Rethrowing exception- JAVA
PPTX
CAP444Unit6ExceptionHandling.pptx
PPTX
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
PPT
Exception Handling1
PPTX
Exception handling
PPTX
Exceptions in C++ Object Oriented Programming.pptx
PPTX
Control structure of c
PPT
Exception Handling Mechanism in .NET CLR
PPT
Java căn bản - Chapter6
PPT
FP 201 Unit 3
PPTX
C Programming: Control Structure
Exceptions in C++exception handling in C++, computer programming.ppt
Object Oriented Programming Using C++: C++ Exception Handling.pptx
Exceptions and Exception Handling in C++
Lecture16
15_exception_handling-1-exception handling .pptx
Exception_Handling_in_C__1701342048430.ppt
22 scheme OOPs with C++ BCS306B_module5.pdf
Unit II Java & J2EE regarding Java application development
exceptiondddddddddddddddddddddddddddddddddddddddd.ppt
Rethrowing exception- JAVA
CAP444Unit6ExceptionHandling.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
Exception Handling1
Exception handling
Exceptions in C++ Object Oriented Programming.pptx
Control structure of c
Exception Handling Mechanism in .NET CLR
Java căn bản - Chapter6
FP 201 Unit 3
C Programming: Control Structure
Ad

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
web development for engineering and engineering
PDF
PPT on Performance Review to get promotions
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT
Project quality management in manufacturing
Construction Project Organization Group 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Embodied AI: Ushering in the Next Era of Intelligent Systems
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT 4 Total Quality Management .pptx
web development for engineering and engineering
PPT on Performance Review to get promotions
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Foundation to blockchain - A guide to Blockchain Tech
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CYBER-CRIMES AND SECURITY A guide to understanding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Project quality management in manufacturing

Exception handling

  • 2.  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.
  • 3. C++ provides three keywords 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 { CCoommbbiinneedd wwiitthh tthhee ttrryy bblloocckk,, tthhee ssyynnttaaxx ooff aann eexxcceeppttiioonn wwoouulldd bbee:: // Try the program flow } catch(Argument) { // Catch the exception }
  • 7. 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"; }
  • 8. 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))
  • 9. NNooww LLeett''ss FFaaccee SSoommee EExxcceeppttiioonnss......
  • 10. #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;
  • 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 FFaaccee AAnn EExxcceeppttiioonn
  • 13. NNooww LLeett''ss FFaaccee AAnnootthheerr EExxcceeppttiioonn......
  • 14. #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; } };
  • 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 FFaaccee AAnn EExxcceeppttiioonn
  • 18. 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.
  • 19. QQ22.. TThheerree ccaann bbee mmuullttiippllee ccaattcchh bblloocckk iinn pprrooggrraamm ?? 1. True 2. False
  • 20. QQ33.. WWhhiicchh ooff tthheessee llaanngguuaaggee ddooeess nnoott ssuuppppoorrtt EExxcceeppttiioonn hhaannddlliinngg ?? 1. Java 2. C++ 3. C
  • 21. Q4. Which of tthheessee ssttaatteemmeennttss iiss ccoorrrreecctt ?? 1. Try 3. Try { throw; { throw }; }; 2. Try 4. Try { throw; { throw } }
  • 22. Q5. WWhhiicchh ooff tthheessee ssttaatteemmeennttss iiss nnoott ccoorrrreecctt ?? 1. Catch(….){} 2. Catch(…){} 3. Catch(const char *message){} 4. Catch(int message){}