SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
OOP USING C++
• When executing C++ code, different errors can occur: coding errors
made by the programmer, errors due to wrong input, or other
unforeseeable things.
• When an error occurs, C++ will normally stop and generate an error
message. The technical term for this is: C++ will throw an exception
(throw an error).
• Exception is an event which occurs during execution of program that
disrupts normal flow of program
• Exception Handling is a process to handle runtime errors.
• We perform exception handling so the normal flow of the application
can be maintained even after runtime errors.
• exception is an event or object which is thrown at runtime.
• All exceptions are derived from exception class. It is a runtime error
which can be handled. If we don't handle the exception, it prints
exception message and terminates the program.
EXCEPTION HANDLING: CONTINUED
• Example of Exception like Divide by zero, Accessing array element
beyond its limit, running out of memory etc.
• Exception handling mechanism consists of followin parts:
1) Find the problem(Hit the Exception)
2) Inform about its occurrence(Throw the exception)
3) Receive error information(Catch the exception)
4) Take proper action(Handle the exception)
WHY EXCEPTION HANDLING
• Seperation of Error handling code from normal code
• functions can handle any exceptions they choose
• Grouping of error types
EXCEPTION HANDLING : CONTINUED
• Exception Handling can be done in 3 ways:
1) try block: try block is used to place the code that may occur
exception. Exception are thrown inside the try block.
2) catch block: catches an exception which is thrown from try block.
The catch keyword indicates the catching of an exception. It is used to
handle the exception. It defines action to be taken when exception
occur
3) throw keyword: throws an exception when a problem is detected.
SYNTAX OF EXCEPTION HANDLING
EXCEPTION HANDLING MECHANISM
EXAMPLE WITHOUT EXCEPTION HANDLING
#include<iostream>
void main()
{
int a,b;
a=10;
b=a/0; //exception occurs
cout<<”result: “<<b;
}
Here program will be terminated you will not get output because exception
occurs at line 6 and flow of program comes out of main() function without
executing line 7.
Exception Handling
#include<iostream>
void main()
{
int a,b;
a=10;
try
{
b=a/0; //exception occurs
}
catch(const char* e)
{
cout<<”Divide by zero error” //exception handler code
}
}
EXAMPLE OF EXCEPTION HANDLING
#include<iostream>
void main()
{
int n1,n2,result;
cout<<”Enter first number: “;
cin>>n1; n1=12
cout<<”Enter second number: “
cin>>n2; n2=20
try
{
if(n2==0)
{
throw n2;
}
else
{
result=n1/n2; 12/20=0.....
}
}
catch(int x) //x=0 n2=0
{
cout<<”Can't divide by “<<x;
}
cout<”End of the program”;
}
Note: Guess the output
1) n1=45, n2=0
2) n1=12, n2=20
EXAMPLE OF EXCEPTION HANDLING
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero
condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
MULTIPLE CATCH EXCEPTION
• Multiple catch statements are
used in case of more than one
exceptions.
• For handling multiple exceptions
we can write multiple catch
statements with different
declarations.
• syntax of multiple catch
statements:
CATCH ALL STATEMENTS
• In some cases it is not feasible to write multiple catch blocks for each
kind of exception in such cases we can write single catch block which
catches all the exceptions
• syntax:
catch(....)
{
statements;
.................
}
EXAMPLE OF EXCEPTION HANDLING 2
EXAMPLE CONTINUED
• GUESS THE OUTPUT FOR FOLLOWING INPUT:
1) n1=20, n2=5
2) n1=5, n2=20
3) n1=-1, n2=20
EXAMPLE OF MULTIPLE CATCH BLOCKS
RETHROWING EXCEPTION
• We can rethrow the exception
where we can have inner and
outer try-catch
statements(nested try-catch).
• Throwing of exception from
inner catch block to outer catch
block is called Rethrowing
Exception
EXAMPLE OF RETHROWING EXCEPTION
DEFINE NEW EXCEPTION
• In this user can create and throw its own exception with the help of
throw statement
EXAMPLE OF CUSTOM EXCEPTION
#include <iostream>
using namespace std;
class Demo {
int num;
public:
demo(int x)
{
try {
if (x == 0){
throw "Zero not allowed ";
}
num = x;
show();
}
catch (const char* exp) {
cout << "Exception caught n ";
cout << exp << endl;
}
}
void show()
{
cout << "Num = " << num << endl;
}
};
void main()
{
Demo d = demo(0);
Demo d1= demo(1);
}
Exception handling c++

More Related Content

What's hot (20)

PPTX
Pointers in c++
Vineeta Garg
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
PPTX
Inline function
Tech_MX
 
PPTX
Packages in java
Elizabeth alexander
 
PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
Templates in c++
ThamizhselviKrishnam
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Strings in c++
Neeru Mittal
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Control Flow Statements
Tarun Sharma
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PPTX
Abstraction java
MahinImran
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
Pointers in c++
Vineeta Garg
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Presentation on-exception-handling
Nahian Ahmed
 
Inline function
Tech_MX
 
Packages in java
Elizabeth alexander
 
OOPS In JAVA.pptx
Sachin33417
 
Super keyword in java
Hitesh Kumar
 
Java Data Types
Spotle.ai
 
Templates in c++
ThamizhselviKrishnam
 
Abstract class in java
Lovely Professional University
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Strings in c++
Neeru Mittal
 
Inheritance in c++
Vineeta Garg
 
Control Flow Statements
Tarun Sharma
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Abstraction java
MahinImran
 
Java exception handling
BHUVIJAYAVELU
 
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 

Similar to Exception handling c++ (20)

PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
PPTX
Lecture 09 Exception Handling(1 ) in c++.pptx
ZenLooper
 
PPT
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
PPT
Exception handling
Iblesoft
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PPTX
Chap2 exception handling
raksharao
 
PPTX
Java-Unit 3- Chap2 exception handling
raksharao
 
PPT
Exceptions in java
Rajkattamuri
 
PPT
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
PPTX
Exceptions overview
Bharath K
 
PPTX
Pi j4.2 software-reliability
mcollison
 
PDF
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
PDF
Exceptions and Exception Handling in C++
IRJET Journal
 
PPTX
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
PPT
exception handling in java.ppt
Varshini62
 
PPTX
43c
Sireesh K
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
CAP444Unit6ExceptionHandling.pptx
SatyajeetGaur2
 
PPT
exceptions in java
javeed_mhd
 
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
Lecture 09 Exception Handling(1 ) in c++.pptx
ZenLooper
 
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
Exception handling
Iblesoft
 
F6dc1 session6 c++
Mukund Trivedi
 
Chap2 exception handling
raksharao
 
Java-Unit 3- Chap2 exception handling
raksharao
 
Exceptions in java
Rajkattamuri
 
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Exceptions overview
Bharath K
 
Pi j4.2 software-reliability
mcollison
 
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
Exceptions and Exception Handling in C++
IRJET Journal
 
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
thirunavukkarasu57
 
exception handling in java.ppt
Varshini62
 
Exception Handling
Reddhi Basu
 
CAP444Unit6ExceptionHandling.pptx
SatyajeetGaur2
 
exceptions in java
javeed_mhd
 
Ad

More from Jayant Dalvi (16)

PPTX
Linux System Administration
Jayant Dalvi
 
PPTX
Linux System Administration
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
Java I/O
Jayant Dalvi
 
PPTX
Information system audit 2
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
java- Abstract Window toolkit
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
Information system audit
Jayant Dalvi
 
PPTX
Information system audit
Jayant Dalvi
 
PPTX
Structured system analysis and design
Jayant Dalvi
 
PPTX
Information system audit
Jayant Dalvi
 
PPTX
Multithreading in Java
Jayant Dalvi
 
PPTX
Object Oriented Programming using C++
Jayant Dalvi
 
Linux System Administration
Jayant Dalvi
 
Linux System Administration
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
Java I/O
Jayant Dalvi
 
Information system audit 2
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
java- Abstract Window toolkit
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
Information system audit
Jayant Dalvi
 
Information system audit
Jayant Dalvi
 
Structured system analysis and design
Jayant Dalvi
 
Information system audit
Jayant Dalvi
 
Multithreading in Java
Jayant Dalvi
 
Object Oriented Programming using C++
Jayant Dalvi
 
Ad

Recently uploaded (20)

PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Horarios de distribución de agua en julio
pegazohn1978
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 

Exception handling c++

  • 2. • When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. • When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error). • Exception is an event which occurs during execution of program that disrupts normal flow of program
  • 3. • Exception Handling is a process to handle runtime errors. • We perform exception handling so the normal flow of the application can be maintained even after runtime errors. • exception is an event or object which is thrown at runtime. • All exceptions are derived from exception class. It is a runtime error which can be handled. If we don't handle the exception, it prints exception message and terminates the program.
  • 4. EXCEPTION HANDLING: CONTINUED • Example of Exception like Divide by zero, Accessing array element beyond its limit, running out of memory etc. • Exception handling mechanism consists of followin parts: 1) Find the problem(Hit the Exception) 2) Inform about its occurrence(Throw the exception) 3) Receive error information(Catch the exception) 4) Take proper action(Handle the exception)
  • 5. WHY EXCEPTION HANDLING • Seperation of Error handling code from normal code • functions can handle any exceptions they choose • Grouping of error types
  • 6. EXCEPTION HANDLING : CONTINUED • Exception Handling can be done in 3 ways: 1) try block: try block is used to place the code that may occur exception. Exception are thrown inside the try block. 2) catch block: catches an exception which is thrown from try block. The catch keyword indicates the catching of an exception. It is used to handle the exception. It defines action to be taken when exception occur 3) throw keyword: throws an exception when a problem is detected.
  • 9. EXAMPLE WITHOUT EXCEPTION HANDLING #include<iostream> void main() { int a,b; a=10; b=a/0; //exception occurs cout<<”result: “<<b; } Here program will be terminated you will not get output because exception occurs at line 6 and flow of program comes out of main() function without executing line 7.
  • 10. Exception Handling #include<iostream> void main() { int a,b; a=10; try { b=a/0; //exception occurs } catch(const char* e) { cout<<”Divide by zero error” //exception handler code } }
  • 11. EXAMPLE OF EXCEPTION HANDLING #include<iostream> void main() { int n1,n2,result; cout<<”Enter first number: “; cin>>n1; n1=12 cout<<”Enter second number: “ cin>>n2; n2=20 try { if(n2==0) { throw n2; } else { result=n1/n2; 12/20=0..... } } catch(int x) //x=0 n2=0 { cout<<”Can't divide by “<<x; } cout<”End of the program”; } Note: Guess the output 1) n1=45, n2=0 2) n1=12, n2=20
  • 12. EXAMPLE OF EXCEPTION HANDLING #include <iostream> using namespace std; double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); } int main () { int x = 50; int y = 0; double z = 0; try { z = division(x, y); cout << z << endl; } catch (const char* msg) { cerr << msg << endl; } return 0; }
  • 13. MULTIPLE CATCH EXCEPTION • Multiple catch statements are used in case of more than one exceptions. • For handling multiple exceptions we can write multiple catch statements with different declarations. • syntax of multiple catch statements:
  • 14. CATCH ALL STATEMENTS • In some cases it is not feasible to write multiple catch blocks for each kind of exception in such cases we can write single catch block which catches all the exceptions • syntax: catch(....) { statements; ................. }
  • 15. EXAMPLE OF EXCEPTION HANDLING 2
  • 16. EXAMPLE CONTINUED • GUESS THE OUTPUT FOR FOLLOWING INPUT: 1) n1=20, n2=5 2) n1=5, n2=20 3) n1=-1, n2=20
  • 17. EXAMPLE OF MULTIPLE CATCH BLOCKS
  • 18. RETHROWING EXCEPTION • We can rethrow the exception where we can have inner and outer try-catch statements(nested try-catch). • Throwing of exception from inner catch block to outer catch block is called Rethrowing Exception
  • 20. DEFINE NEW EXCEPTION • In this user can create and throw its own exception with the help of throw statement
  • 21. EXAMPLE OF CUSTOM EXCEPTION #include <iostream> using namespace std; class Demo { int num; public: demo(int x) { try { if (x == 0){ throw "Zero not allowed "; } num = x; show(); } catch (const char* exp) { cout << "Exception caught n "; cout << exp << endl; } } void show() { cout << "Num = " << num << endl; } }; void main() { Demo d = demo(0); Demo d1= demo(1); }