際際滷

際際滷Share 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 programs 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 trys 
closing bracket and the catch section. 
2. There can be any code between the trys closing 
bracket and the catch section. 
3. There may be any code between the trys closing 
bracket and the catch section. 
4. There must be any code between the trys 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
Ad

Recommended

PPTX
Exception handling c++
Jayant Dalvi
PPTX
C++ ala
Megha Patel
PDF
Exception handling
Pranali Chaudhari
PPT
Exception handling and templates
farhan amjad
PPTX
Exception Handling in C++
Deepak Tathe
PPT
Exceptions in c++
Kuntal Bhowmick
PDF
Exception Handling in the C++ Constructor
Somenath Mukhopadhyay
PPTX
exception handling in cpp
gourav kottawar
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
PPTX
Exception handling chapter15
Kumar
PPT
Exception handling
pooja kumari
PDF
14 exception handling
jigeno
PPT
Handling Exceptions In C &amp; C++[Part A]
ppd1961
PPTX
What is Exception Handling?
Syed Bahadur Shah
PPSX
Exception Handling
Reddhi Basu
PPTX
Exception handling
Abhishek Pachisia
PPT
Exception handling
Iblesoft
PPTX
Exception handling in java
yugandhar vadlamudi
PPT
Week7 exception handling
Alpesh Oza
PPTX
130410107010 exception handling
Hemant Chetwani
PPT
Unit iii
snehaarao19
PDF
EXCEPTION HANDLING in C++
Prof Ansari
PPT
Exception Handling in JAVA
SURIT DATTA
PPS
Exception handling in c programming
Raza Najam
PDF
Exception handling
Ravi Sharda
PPT
Exception handling in java
Pratik Soares
PPTX
Exception handling
Waqas Abbasi
PPTX
Exception handling in c++
imran khan
PPT
F6dc1 session6 c++
Mukund Trivedi
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217

More Related Content

What's hot (20)

PPTX
Exception Handling in object oriented programming using C++
Janki Shah
PPTX
Exception handling chapter15
Kumar
PPT
Exception handling
pooja kumari
PDF
14 exception handling
jigeno
PPT
Handling Exceptions In C &amp; C++[Part A]
ppd1961
PPTX
What is Exception Handling?
Syed Bahadur Shah
PPSX
Exception Handling
Reddhi Basu
PPTX
Exception handling
Abhishek Pachisia
PPT
Exception handling
Iblesoft
PPTX
Exception handling in java
yugandhar vadlamudi
PPT
Week7 exception handling
Alpesh Oza
PPTX
130410107010 exception handling
Hemant Chetwani
PPT
Unit iii
snehaarao19
PDF
EXCEPTION HANDLING in C++
Prof Ansari
PPT
Exception Handling in JAVA
SURIT DATTA
PPS
Exception handling in c programming
Raza Najam
PDF
Exception handling
Ravi Sharda
PPT
Exception handling in java
Pratik Soares
PPTX
Exception handling
Waqas Abbasi
PPTX
Exception handling in c++
imran khan
Exception Handling in object oriented programming using C++
Janki Shah
Exception handling chapter15
Kumar
Exception handling
pooja kumari
14 exception handling
jigeno
Handling Exceptions In C &amp; C++[Part A]
ppd1961
What is Exception Handling?
Syed Bahadur Shah
Exception Handling
Reddhi Basu
Exception handling
Abhishek Pachisia
Exception handling
Iblesoft
Exception handling in java
yugandhar vadlamudi
Week7 exception handling
Alpesh Oza
130410107010 exception handling
Hemant Chetwani
Unit iii
snehaarao19
EXCEPTION HANDLING in C++
Prof Ansari
Exception Handling in JAVA
SURIT DATTA
Exception handling in c programming
Raza Najam
Exception handling
Ravi Sharda
Exception handling in java
Pratik Soares
Exception handling
Waqas Abbasi
Exception handling in c++
imran khan

Similar to Exception handling (20)

PPT
F6dc1 session6 c++
Mukund Trivedi
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
PPTX
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
PDF
Exceptions and Exception Handling in C++
IRJET Journal
PPT
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
PPTX
Lecture 1 Try Throw Catch.pptx
VishuSaini22
PPTX
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
PPTX
Object Oriented Programming Using C++: C++ Exception Handling.pptx
RashidFaridChishti
PDF
Exceptions ref
. .
PPT
Exception handling
zindadili
PPT
Handling
Amit Vats
PPTX
6-Exception Handling and Templates.pptx
SatyamMishra237306
PPT
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
PDF
$Cash
ErandaMoney
PDF
$Cash
ErandaMoney
PPT
Lecture16
elearning_portal
PPT
Week7_ExceptionHandling.ppt
SatyaKumari18
PPT
Exception and Error Handling in C++ - A detailed Presentation
debasisdas225831
PPTX
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
PPT
UNIT III.ppt
Ajit Mali
F6dc1 session6 c++
Mukund Trivedi
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
Exceptions in C++ Object Oriented Programming.pptx
estorebackupr
Exceptions and Exception Handling in C++
IRJET Journal
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
Lecture 1 Try Throw Catch.pptx
VishuSaini22
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
Object Oriented Programming Using C++: C++ Exception Handling.pptx
RashidFaridChishti
Exceptions ref
. .
Exception handling
zindadili
Handling
Amit Vats
6-Exception Handling and Templates.pptx
SatyamMishra237306
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
$Cash
ErandaMoney
$Cash
ErandaMoney
Lecture16
elearning_portal
Week7_ExceptionHandling.ppt
SatyaKumari18
Exception and Error Handling in C++ - A detailed Presentation
debasisdas225831
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
ssusercd11c4
UNIT III.ppt
Ajit Mali
Ad

Recently uploaded (20)

PDF
惠惘惘 惺 悋惠忰 悋惆悋 惠惆 悋悋悄 忰 悴悋忰.pdf
忰惆 惶惶 惠惠悸
PPTX
Work at Height training for workers .pptx
cecos12
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
PPTX
WHO And BIS std- for water quality .pptx
dhanashree78
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
PDF
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
PPTX
Introduction to Python Programming Language
merlinjohnsy
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
PDF
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
PPTX
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego L坦pez-de-Ipi単a Gonz叩lez-de-Artaza
PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
惠惘惘 惺 悋惠忰 悋惆悋 惠惆 悋悋悄 忰 悴悋忰.pdf
忰惆 惶惶 惠惠悸
Work at Height training for workers .pptx
cecos12
Precooling and Refrigerated storage.pptx
ThongamSunita
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
WHO And BIS std- for water quality .pptx
dhanashree78
Computer network Computer network Computer network Computer network
Shrikant317689
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
Introduction to Python Programming Language
merlinjohnsy
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
PRIZ Academy - Process functional modelling
PRIZ Guru
CST413 KTU S7 CSE Machine Learning Introduction Parameter Estimation MLE MAP ...
resming1
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego L坦pez-de-Ipi単a Gonz叩lez-de-Artaza
01-introduction to the ProcessDesign.pdf
StiveBrack
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
Ad

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 programs 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 trys closing bracket and the catch section. 2. There can be any code between the trys closing bracket and the catch section. 3. There may be any code between the trys closing bracket and the catch section. 4. There must be any code between the trys 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){}