SlideShare a Scribd company logo
Exception Handling
Exceptions
• Run time errors that a program may encounter while
executing.
• Example
– Divide by Zero
– Access elements out of size
Drawbacks
• Abnormal program termination when the exception
occurs.
• The user can not find where the exception occurs.
Exception Handling
• Is a mechanism of detecting and reporting
exceptional circumstance so that an
appropriate action can be taken.
• To transmit information about the problems.
Task
• Find the problem(Hit the exception-Try)
• Send the error information(Throw the
exception)
• Receive the error information(Catch the
exception)
• Take corrective actions.(handle the exception)
Exception handling mechanism is done using
Try
Is a block of statement which may generate
exceptions.
try{}
Catch
Is a block which catches and handles exception by
taking appropriate actions.
catch(datatype arg){}
Throw
Is a statement , to throw the detected exception to
catch block.
throw exception;
Exception_Handling_in_C__1701342048430.ppt
• Throwing Mechanism
– throw(exception)
– throw exception
– throw //Rethrowing exception- used to rethrow
the exception caught without processing it.
– exceptions are variable of any type.
– It should be caught by catch block which matches the
argument.
Multiple catch statement
try
{}
catch(arg)
{}
catch(arg)
{}
catch(arg)
{}
Catch all exceptions
Not possible to for the design of
independent catch handlers.
catch(...){} //called default catch to catch
all exceptions.
Exception_Handling_in_C__1701342048430.ppt
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter values";
try{
cin>>a>>b;
if(a<0 && b<0)
throw 0;
else if(b==0)
throw('x');
else{
c=a/b;
cout<<c<<endl;}}
catch(int n)
{
cout<<"enter positive values";
}
catch(char c)
{
cout<<"enter non zero value";
return 0;
}
}
Exception Handling-multiple catches
Exception_Handling_in_C__1701342048430.ppt
#include<iostream>
using namespace std;
int bsquare(int a)
{
try{
if(a==0)
throw 1;
else if(a<0)
throw 3.14f;
else
return (a*a);
}
catch(int s)
{
cout<<"enter non zero value";
}
catch(float f)
{
throw; //Rethrowing Exception
}}
int main()
{
int x;
cout<<"enter values";
cin>>x;
try{
cout<<bsquare(x);
}
catch(...) //Default Catch
{
cout<<"Rethrouing exception caught"<<endl;
}
}
Exception Class
class Exclass
{
};
try
{
throw Exclass(args);
}
catch(Exclass obj)
{
}
Exception Class-user defined exception
#include<iostream>
using namespace std;
class Bexception
{
int ecode;
string emsg;
public:
Bexception(int a,string s)
{
ecode=a;
emsg=s;
}
void display()
{
cout<<ecode<<" : "<<emsg<<endl;
}
};
int main()
{
int x;
cout<<"enter values";
cin>>x;
try{
if(x==0)
throw Bexception(1000,"Zero value
exception");
else
cout<<(x*x);
}
catch(Bexception B)
{
B.display();
}
catch(...)
{
cout<<"Rethrouing exception
caught"<<endl;
}
}
Programs
Create a class namely “stack” demonstrate the
following
• Stack overflow exception
• Stack underflow exception
• Empty stack exception while trying to pop before doing
push
• To handle rethrown exception.
Create a class namely “average” demonstrate the
following
• Divide by zero exception
• Negative value exception
• Default exception
• To handle rethrown exception.
class Bstack
{
private:
int st[3];
int top,flag;
public:
Bstack()
{
top=0;flag=0;
}
int pop()
{
try{
if(flag==0)
throw 3.14f;
top--;
if(top<0)
throw('c');
else
return st[top];
}
catch(char c)
{
cout<<"Stack Under flow
Exception"<<endl;
}
catch(float ff)
{
cout<<"empty stack
exception"<<endl;
throw; }}};
main()
{
Bstack B;
try{
B.pop();}
catch(...)
{
cout<<"Rethrowing
Exception"<<endl;}
B.push(20);
B.push(30);
B.push(40);
B.push(50);
cout<<B.pop()<<endl;
cout<<B.pop()<<endl;
cout<<B.pop()<<endl;
cout<<B.pop()<<endl;
}
void push(int n)
{
flag=1;
try{
if(top>=3)
throw(0);
else
{
st[top]=n;
top++;}
}
catch(int z)
{
cout<<"Stack overflow
exception"<<endl;
}
}
[bavani@mepcolinux ~]$./a.out
empty stack exception
Rethrowing Exception
Stack overflow exception
40
30
20
Stack Under flow Exception
0
class Average
{
int a[100],n;
public:
void get()
{
cout<<"Enter N";
try{
cin>>n;
if(n==0)
throw 10;
else
{
cout<<"Enter Values";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
}
catch(int s)
{
cout<<"Divide by Zero Exceptionn";
throw;
}
}
void average()
{
int sum=0;
try{
for(int i=0;i<n;i++)
{
if(a[i]<0)
throw 3.14f;
else
sum=sum+a[i];
}
}
catch(float ff)
{
cout<<"Negative value Exceptionn";
throw;
}
}
};
int main()
{
Average A;
L1:
try{
A.get();
A.average();
}
catch(int nn)
{
cout<<"Rethrowing Exception in Mainn";
goto L1;
}
catch(...)
{
cout<<"Default Catchn";
}
}
[bavani@mepcolinux ~]$./a.out
Enter N0
Divide by Zero Exception
Rethrowing Exception in main
Enter N3
Enter Values1
2
-3
Negative value Exception
Default Catch

More Related Content

PPT
Exceptions in C++exception handling in C++, computer programming.ppt
PPTX
Exception Handling in object oriented programming using C++
PPTX
Exception handling
PPTX
Object Oriented Programming Using C++: C++ Exception Handling.pptx
PPT
Exception handling
PPTX
Exceptions in C++ Object Oriented Programming.pptx
PPTX
Unit II Java & J2EE regarding Java application development
PPTX
C++ ala
Exceptions in C++exception handling in C++, computer programming.ppt
Exception Handling in object oriented programming using C++
Exception handling
Object Oriented Programming Using C++: C++ Exception Handling.pptx
Exception handling
Exceptions in C++ Object Oriented Programming.pptx
Unit II Java & J2EE regarding Java application development
C++ ala

Similar to Exception_Handling_in_C__1701342048430.ppt (20)

PPT
Exception handling and templates
PPTX
Rethrowing exception- JAVA
PPTX
Lecture 09 Exception Handling(1 ) in c++.pptx
PPTX
CAP444Unit6ExceptionHandling.pptx
PDF
Exception handling
PPSX
Exception Handling
PPTX
exception handling in cpp
PPTX
Namespaces
PPTX
Chapter v(error)
PDF
Exceptions and Exception Handling in C++
PPT
Exceptions in c++
PPT
Unit iii
PPTX
Exception handling
PPT
Handling
PDF
17 exception handling - ii
PPT
UNIT III.ppt
PPT
UNIT III (2).ppt
PPT
Exception Handling1
PPTX
Java exception handling
Exception handling and templates
Rethrowing exception- JAVA
Lecture 09 Exception Handling(1 ) in c++.pptx
CAP444Unit6ExceptionHandling.pptx
Exception handling
Exception Handling
exception handling in cpp
Namespaces
Chapter v(error)
Exceptions and Exception Handling in C++
Exceptions in c++
Unit iii
Exception handling
Handling
17 exception handling - ii
UNIT III.ppt
UNIT III (2).ppt
Exception Handling1
Java exception handling
Ad

More from arunkumarg271 (9)

PDF
Measurement_of_Blood_Pressure_and_Heart_Sound_1722949324827.pdf
PPTX
Power system analysis in load flow analysis.pptx
PPTX
Block chain Technology with digital and social impacts.pptx
PDF
Hydro_Electric_Power_plant_1738642644263.pdf
PPTX
it's about the swing programs in java language
PPTX
it is about the abstract classes in java
PPTX
it is the quick gest about the interfaces in java
PPTX
EM BRAKES.pptx
PPTX
GRT.pptx
Measurement_of_Blood_Pressure_and_Heart_Sound_1722949324827.pdf
Power system analysis in load flow analysis.pptx
Block chain Technology with digital and social impacts.pptx
Hydro_Electric_Power_plant_1738642644263.pdf
it's about the swing programs in java language
it is about the abstract classes in java
it is the quick gest about the interfaces in java
EM BRAKES.pptx
GRT.pptx
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Insiders guide to clinical Medicine.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
human mycosis Human fungal infections are called human mycosis..pptx
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
VCE English Exam - Section C Student Revision Booklet
Sports Quiz easy sports quiz sports quiz
Anesthesia in Laparoscopic Surgery in India

Exception_Handling_in_C__1701342048430.ppt

  • 2. Exceptions • Run time errors that a program may encounter while executing. • Example – Divide by Zero – Access elements out of size Drawbacks • Abnormal program termination when the exception occurs. • The user can not find where the exception occurs.
  • 3. Exception Handling • Is a mechanism of detecting and reporting exceptional circumstance so that an appropriate action can be taken. • To transmit information about the problems. Task • Find the problem(Hit the exception-Try) • Send the error information(Throw the exception) • Receive the error information(Catch the exception) • Take corrective actions.(handle the exception)
  • 4. Exception handling mechanism is done using Try Is a block of statement which may generate exceptions. try{} Catch Is a block which catches and handles exception by taking appropriate actions. catch(datatype arg){} Throw Is a statement , to throw the detected exception to catch block. throw exception;
  • 6. • Throwing Mechanism – throw(exception) – throw exception – throw //Rethrowing exception- used to rethrow the exception caught without processing it. – exceptions are variable of any type. – It should be caught by catch block which matches the argument.
  • 8. Catch all exceptions Not possible to for the design of independent catch handlers. catch(...){} //called default catch to catch all exceptions.
  • 10. #include<iostream> using namespace std; int main() { int a,b,c; cout<<"enter values"; try{ cin>>a>>b; if(a<0 && b<0) throw 0; else if(b==0) throw('x'); else{ c=a/b; cout<<c<<endl;}} catch(int n) { cout<<"enter positive values"; } catch(char c) { cout<<"enter non zero value"; return 0; } } Exception Handling-multiple catches
  • 12. #include<iostream> using namespace std; int bsquare(int a) { try{ if(a==0) throw 1; else if(a<0) throw 3.14f; else return (a*a); } catch(int s) { cout<<"enter non zero value"; } catch(float f) { throw; //Rethrowing Exception }} int main() { int x; cout<<"enter values"; cin>>x; try{ cout<<bsquare(x); } catch(...) //Default Catch { cout<<"Rethrouing exception caught"<<endl; } }
  • 13. Exception Class class Exclass { }; try { throw Exclass(args); } catch(Exclass obj) { }
  • 14. Exception Class-user defined exception #include<iostream> using namespace std; class Bexception { int ecode; string emsg; public: Bexception(int a,string s) { ecode=a; emsg=s; } void display() { cout<<ecode<<" : "<<emsg<<endl; } }; int main() { int x; cout<<"enter values"; cin>>x; try{ if(x==0) throw Bexception(1000,"Zero value exception"); else cout<<(x*x); } catch(Bexception B) { B.display(); } catch(...) { cout<<"Rethrouing exception caught"<<endl; } }
  • 15. Programs Create a class namely “stack” demonstrate the following • Stack overflow exception • Stack underflow exception • Empty stack exception while trying to pop before doing push • To handle rethrown exception. Create a class namely “average” demonstrate the following • Divide by zero exception • Negative value exception • Default exception • To handle rethrown exception.
  • 16. class Bstack { private: int st[3]; int top,flag; public: Bstack() { top=0;flag=0; } int pop() { try{ if(flag==0) throw 3.14f; top--; if(top<0) throw('c'); else return st[top]; } catch(char c) { cout<<"Stack Under flow Exception"<<endl; } catch(float ff) { cout<<"empty stack exception"<<endl; throw; }}}; main() { Bstack B; try{ B.pop();} catch(...) { cout<<"Rethrowing Exception"<<endl;} B.push(20); B.push(30); B.push(40); B.push(50); cout<<B.pop()<<endl; cout<<B.pop()<<endl; cout<<B.pop()<<endl; cout<<B.pop()<<endl; } void push(int n) { flag=1; try{ if(top>=3) throw(0); else { st[top]=n; top++;} } catch(int z) { cout<<"Stack overflow exception"<<endl; } }
  • 17. [bavani@mepcolinux ~]$./a.out empty stack exception Rethrowing Exception Stack overflow exception 40 30 20 Stack Under flow Exception 0
  • 18. class Average { int a[100],n; public: void get() { cout<<"Enter N"; try{ cin>>n; if(n==0) throw 10; else { cout<<"Enter Values"; for(int i=0;i<n;i++) { cin>>a[i]; } } } catch(int s) { cout<<"Divide by Zero Exceptionn"; throw; } } void average() { int sum=0; try{ for(int i=0;i<n;i++) { if(a[i]<0) throw 3.14f; else sum=sum+a[i]; } } catch(float ff) { cout<<"Negative value Exceptionn"; throw; } } };
  • 19. int main() { Average A; L1: try{ A.get(); A.average(); } catch(int nn) { cout<<"Rethrowing Exception in Mainn"; goto L1; } catch(...) { cout<<"Default Catchn"; } } [bavani@mepcolinux ~]$./a.out Enter N0 Divide by Zero Exception Rethrowing Exception in main Enter N3 Enter Values1 2 -3 Negative value Exception Default Catch