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

PPTX
Polymorphism in c++(ppt)
PPTX
Exception Handling in object oriented programming using C++
PPTX
Azure Compute, Networking and Storage Overview
PPTX
Exception handling in c++
PDF
JavaScript - Chapter 11 - Events
PPTX
Report Writing PPT
PPT
Operator precedence and associativity
PPTX
Political parties of india
Polymorphism in c++(ppt)
Exception Handling in object oriented programming using C++
Azure Compute, Networking and Storage Overview
Exception handling in c++
JavaScript - Chapter 11 - Events
Report Writing PPT
Operator precedence and associativity
Political parties of india

What's hot (20)

PPT
Operator Overloading
PPTX
Interface in java
PPTX
Static Data Members and Member Functions
PPTX
Constructor in java
PDF
Object oriented programming c++
PDF
Function overloading ppt
PPTX
PPTX
Inline function
PDF
Exception handling
PPTX
Oop c++class(final).ppt
PDF
Java I/o streams
PPTX
Abstract class in c++
PPTX
Programming in c Arrays
PPT
Abstract class in java
PDF
Classes and objects
PPTX
Packages in java
PPTX
java interface and packages
PPTX
Polymorphism In c++
PPTX
Pointer in C++
PPTX
classes and objects in C++
Operator Overloading
Interface in java
Static Data Members and Member Functions
Constructor in java
Object oriented programming c++
Function overloading ppt
Inline function
Exception handling
Oop c++class(final).ppt
Java I/o streams
Abstract class in c++
Programming in c Arrays
Abstract class in java
Classes and objects
Packages in java
java interface and packages
Polymorphism In c++
Pointer in C++
classes and objects in C++
Ad

Similar to Exception handling c++ (20)

PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
PPTX
Lecture 09 Exception Handling(1 ) in c++.pptx
PPT
Exceptions in C++exception handling in C++, computer programming.ppt
PPT
Exception handling
PPT
F6dc1 session6 c++
PPTX
Java-Unit 3- Chap2 exception handling
PPTX
Chap2 exception handling
PPT
Exceptions in java
PPT
Exception Handling Exception Handling Exception Handling
PPTX
Exceptions overview
PPTX
Pi j4.2 software-reliability
PDF
Ch-1_5.pdf this is java tutorials for all
PDF
Exceptions and Exception Handling in C++
PPTX
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
PPT
exception handling in java.ppt
PPTX
PPSX
Exception Handling
PPTX
CAP444Unit6ExceptionHandling.pptx
PPT
exceptions in java
22 scheme OOPs with C++ BCS306B_module5.pdf
Lecture 09 Exception Handling(1 ) in c++.pptx
Exceptions in C++exception handling in C++, computer programming.ppt
Exception handling
F6dc1 session6 c++
Java-Unit 3- Chap2 exception handling
Chap2 exception handling
Exceptions in java
Exception Handling Exception Handling Exception Handling
Exceptions overview
Pi j4.2 software-reliability
Ch-1_5.pdf this is java tutorials for all
Exceptions and Exception Handling in C++
UNIT 2 UNIT 3 OBJECT ORIENTED PROGRAMMING
exception handling in java.ppt
Exception Handling
CAP444Unit6ExceptionHandling.pptx
exceptions in java
Ad

More from Jayant Dalvi (16)

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

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Trump Administration's workforce development strategy
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
Cell Types and Its function , kingdom of life
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
RMMM.pdf make it easy to upload and study
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Trump Administration's workforce development strategy
2.FourierTransform-ShortQuestionswithAnswers.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
What if we spent less time fighting change, and more time building what’s rig...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Updated Idioms and Phrasal Verbs in English subject
Cell Types and Its function , kingdom of life
Yogi Goddess Pres Conference Studio Updates
RMMM.pdf make it easy to upload and study
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf

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); }