SlideShare a Scribd company logo
Exception handling
• Exception handling allows us to manage
run-time errors in an orderly fashion
• program can automatically invoke an
error-handling routine when an error
occurs.
• Advantage:
– Automation of error-handling code
Exception Handling Fundamentals
• three basic keywords: try, catch, and throw.
• program statements that you want to monitor for
exceptions are contained in a try block.
• If an exception (i.e., an error) occurs within the
try block, it is thrown (using throw).
• Functions called from within a try block may also
throw an exception.
• The exception is caught, using catch, and
processed.
Syntax
try {
// try block
}
catch (type1 arg) {
// catch block
}
catch (type2 arg) {
// catch block
}
catch (type3 arg) {
// catch block
}
..
.
catch (typeN arg) {
// catch block
}
The syntax for throw statement is :
throw exception
Handling Exceptions
• When an exception is thrown, it is caught
by its corresponding catch statement.
• There can be more than one catch
statement associated with a try.
• If the data type specified by a catch
matches that of the exception, then that
catch statement is executed (and all
others are bypassed).
Unhandled Exceptions
• If there is no applicable catch statement,
an abnormal program termination may
occur.
• For an unhandled exception, standard
library function terminate() is invoked.
• By default, terminate() calls abort() to
stop the program
Example:
#include <iostream>
using namespace std;
int main()
{
cout << "Startn";
try { // start a try block
cout << "Inside try blockn";
throw 100; // throw an error
cout << "This will not execute";
}
catch (int i) { // catch an error
cout << "Caught an exception --
value is: ";
cout << i << "n";
}
cout << "End";
}
• Output:
Start
Inside try block
Caught an exception -- value is:
100
End
• Once an exception has been thrown, control passes to the
catch expression and the try block is terminated.
• catch is not called. Rather, program execution is
transferred to it.
• The program's stack is automatically reset.
• The statements following the throw will never execute.
• If the error can be fixed, execution will continue after catch,
else a catch block will terminate the program with a call to
exit() or abort() .
The type of the exception must match the type specified in a catch
statement
The exception will not be caught and abnormal termination will
occur
#include <iostream>
using namespace std;
int main()
{
cout << "Startn";
try { // start a try block
cout << "Inside try blockn";
throw 100; // throw an error
cout << "This will not execute";
}
catch (double i) { // won't work for an int
exception
cout << "Caught an exception -- value is: ";
cout << i << "n";
}
cout << "End";
return 0;
}
Output
Start
Inside try block
Abnormal program termination
Throwing an exception from a function outside the
try block.
#include <iostream>
using namespace std;
void Xtest(int test)
{
cout << "Inside Xtest, test is: " << test << "
n";
if(test) throw test;
}
int main()
{
cout << "Startn";
try { // start a try block
cout << "Inside try blockn";
Xtest(0);
Xtest(1);
Xtest(2);
}
catch (int i) { // catch an error
cout << "Caught an exception -- value is: ";
cout << i << "n";
}
cout << "End";
return 0;
}
Output:
Start
Inside try block
Inside Xtest, test is: 0
Inside Xtest, test is: 1
Caught an exception -- value is: 1
End
Local try block
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << 'n';
}
}
int main()
{
cout << "Startn";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
Output:
Start
Caught Exception #: 1
Caught Exception #: 2
Caught Exception #: 3
End
Catching Class Types
• Generally an exception can be of any type.
• In real-world programs, most exceptions
will be class types rather than built-in
types.
• common reason - to create an object that
describes the error.
• It can be used by the exception handler to
help it process the error.
// Catching class type exceptions.
#include <iostream>
#include <cstring>
using namespace std;
class MyException {
public:
char str_what[80];
int what;
MyException() { *str_what = 0; what = 0; }
MyException(char *s, int e) {
strcpy(str_what, s);
what = e;
}
};
int main()
{
int i;
try {
cout << "Enter a positive number: ";
cin >> i;
if(i<0)
throw MyException("Not Positive", i);
}
catch (MyException e) { // catch an error
cout << e.str_what << ": ";
cout << e.what << "n";
}
return 0;
}
Output:
Enter a positive number: -4
Not Positive: -4
Using Multiple catch Statements
• There can be more than one catch
associated with a try.
• Each catch must catch a different type of
exception.
#include <iostream>
using namespace std;
// Different types of exceptions can be
caught.
void Xhandler(int test)
{
try{
if(test) throw test;
else throw "Value is zero";
}
catch(int i) {
cout << "Caught Exception #: " << i << 'n';
}
catch(const char *str) {
cout << "Caught a string: ";
cout << str << 'n';
}
}
int main()
{
cout << "Startn";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
Output:
Start
Caught Exception #: 1
Caught Exception #: 2
Caught a string: Value is zero
Caught Exception #: 3
End
Handling Derived-Class Exceptions
• order of catch statements is most
important while inheriting.
• because a catch clause for a base class
will also match any class derived from that
base.
• to catch exceptions of both a base class
and a derived class, put the derived class
first in the catch sequence.
// Catching derived classes.
#include <iostream>
using namespace std;
class B {
};
class D: public B {
};
int main()
{
D derived;
try {
throw derived;
}
catch(B b) {
cout << "Caught a base class.n";
}
catch(D d) {
cout << "This won't execute.n";
}
return 0;
}
• In this example the catch clause of the
base class will always be executed.
• reverse the order of the catch clauses
Exception Handling Options
Additional features and nuances that make it
easier and more convenient to use are,
• Catching All Exceptions
• Restricting Exceptions
• Rethrowing an Exception
Catching All Exceptions
• This catch block will catch all kind of
exceptions
• Syntax:
catch(...) {
// process all exceptions
}
// This example catches all exceptions.
#include <iostream>
using namespace std;
void Xhandler(int test)
{
try{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
catch(...) { // catch all exceptions
cout << "Caught One!n";
}
}
int main()
{
cout << "Startn";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "End";
return 0;
}
Output:
Start
Caught One!
Caught One!
Caught One!
End
This block can be made the last catch block to catch all the
left out exceptions
// This example uses catch(...) as a
default.
#include <iostream>
using namespace std;
void Xhandler(int test)
{
try{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
catch(int i) { // catch an int exception
cout << "Caught an integern";
}
catch(...) { // catch all other exceptions
cout << "Caught One!n";
}
}
int main()
{
cout << "Startn";
Xhandler(0);
Xhandler(1);
Xhandler(2);
cout << "End";
return 0;
}
OUTPUT:
Start
Caught an integer
Caught One!
Caught One!
End
Restricting Exceptions
• Exceptions thrown outside the functions
can be restricted.
• To accomplish these restrictions, you must
add a throw clause to a function definition.
• Syntax:
ret-type func-name(arg-list) throw(type-list)
{
//..
}
• Only the types specified in the type-list can be thrown as
exceptions.
• Throwing any other type of expression will cause abnormal
program termination
• Attempting to throw an exception that is not supported by a function
will cause the standard library function unexpected() to be called
unexpected() -> abort()
// Restricting function throw types.
#include <iostream>
using namespace std;
// This function can only throw ints, chars,
and doubles.
void Xhandler(int test) throw(int, char,
double)
{
if(test==0) throw test; // throw int
if(test==1) throw 'a'; // throw char
if(test==2) throw 123.23; // throw double
}
int main()
{
cout << "startn";
try{
Xhandler(0); // also, try passing 1 and 2 to
Xhandler()
}
catch(int i) {
cout << "Caught an integern";
}
catch(char c) {
cout << "Caught charn";
}
catch(double d) {
cout << "Caught doublen";
}
cout << "end";
return 0;
}
// This function can throw NO exceptions!
void Xhandler(int test) throw()
{
/* The following statements no longer work. Instead, they
will cause an abnormal program termination. */
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}
Rethrowing an Exception
• An exception can be thrown back, by
calling throw, by itself, with no exception.
• the current exception to be passed on to
an outer try/catch sequence.
• This allows multiple handlers access to the
exception.
// Example of "rethrowing" an
exception.
#include <iostream>
using namespace std;
void Xhandler()
{
try {
throw "hello"; // throw a char *
}
catch(const char *) { // catch a
char *
cout << "Caught char * inside
Xhandlern";
throw ; // rethrow char * out of
function
}
}
int main()
{
cout << "Startn";
try{
Xhandler();
}
catch(const char *) {
cout << "Caught char * inside
mainn";
}
cout << "End";
return 0;
}
Output:
Start
Caught char * inside Xhandler
Caught char * inside main
End
Understanding terminate() and unexpected()
• These functions are supplied by the
Standard C++ library
• These functions require the header
<exception>.
• Prototypes
– void terminate( );
– void unexpected( );
• The terminate() function is called whenever program
attempts to rethrow an exception when no exception was
originally thrown.
• Eg:
Unwinding the stack because of an exception, a
destructor for an object may throw an exception
• terminate() is the handler of last resort when no other
handlers for an exception are available.
terminate() -> abort()
• The unexpected() function is called when a
function attempts to throw an exception
• that is not allowed by its throw
unexpected() -> terminate() .
Setting the Terminate and Unexpected
Handlers
• Syntax:
terminate_handler set_terminate(terminate_handler newhandler) throw();
unexpected_handler set_unexpected(unexpected_handler newhandler) throw();
Both set_terminate() and set_unexpected() require the
header <exception>.
// Set a new terminate handler.
#include <iostream>
#include <cstdlib>
#include <exception>
using namespace std;
void my_Thandler() {
cout << "Inside new terminate handlern";
abort();
}
int main()
{
// set a new terminate handler
set_terminate(my_Thandler);
try {
cout << "Inside try blockn";
throw 100; // throw an error
}
catch (double i) { // won't catch an int exception
// ...
}
return 0;
}
Output:
Inside try block
Inside new terminate handler
abnormal program termination
The uncaught_exception( ) Function
bool uncaught_exception( );
True -> if an exception has been thrown
but not yet caught.
False -> Exception is caught.
The exception and bad_exception Classes
• Exceptions thrown by C++ standard library will be object
derived from the base class exception.
• An object of the class bad_exception can be thrown by
the unexpected handler
• These classes require the header <exception.h>.
Applying Exception Handling
• Exception handling is designed to provide a
structured means to handle abnormal events.
• The error handler must do something rational
when an error occurs.
#include <iostream>
using namespace std;
void divide(double a, double b);
int main()
{
double i, j;
do {
cout << "Enter numerator (0 to
stop): ";
cin >> i;
cout << "Enter denominator: ";
cin >> j;
divide(i, j);
} while(i != 0);
return 0;
}
void divide(double a, double b)
{
try {
if(!b) throw b; // check for divide-by-zero
cout << "Result: " << a/b << endl;
}
catch (double b) {
cout << "Can't divide by zero.n";
}
}

More Related Content

Similar to Exceptions in C++exception handling in C++, computer programming.ppt (20)

PPTX
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
PPTX
Exception handling c++
Jayant Dalvi
 
PDF
Exceptions ref
. .
 
PPTX
Object Oriented Programming Using C++: C++ Exception Handling.pptx
RashidFaridChishti
 
PPSX
Exception Handling
Reddhi Basu
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PPTX
Exception handling
Waqas Abbasi
 
PPTX
Exception handling
zindadili
 
DOCX
Catch and throw blocks
ashrafkhan12345
 
PPTX
C++ ala
Megha Patel
 
PPT
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PPT
Handling
Amit Vats
 
PPTX
exception handling
rajshreemuthiah
 
PPTX
Web technology
Siva Priya
 
PDF
$Cash
ErandaMoney
 
PDF
$Cash
ErandaMoney
 
PPTX
Exception handling chapter15
Kumar
 
PPT
Exception handling and templates
farhan amjad
 
PPTX
Exception handling
Abhishek Pachisia
 
Lecture 3.1.1 Try Throw Catch.pptx
sunilsoni446112
 
Exception handling c++
Jayant Dalvi
 
Exceptions ref
. .
 
Object Oriented Programming Using C++: C++ Exception Handling.pptx
RashidFaridChishti
 
Exception Handling
Reddhi Basu
 
Exception Handling in object oriented programming using C++
Janki Shah
 
Exception handling
Waqas Abbasi
 
Exception handling
zindadili
 
Catch and throw blocks
ashrafkhan12345
 
C++ ala
Megha Patel
 
Exception_Handling_in_C__1701342048430.ppt
arunkumarg271
 
F6dc1 session6 c++
Mukund Trivedi
 
Handling
Amit Vats
 
exception handling
rajshreemuthiah
 
Web technology
Siva Priya
 
Exception handling chapter15
Kumar
 
Exception handling and templates
farhan amjad
 
Exception handling
Abhishek Pachisia
 

Recently uploaded (20)

PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
epi editorial commitee meeting presentation
MIPLM
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Horarios de distribución de agua en julio
pegazohn1978
 
Difference between write and update in odoo 18
Celine George
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Ad

Exceptions in C++exception handling in C++, computer programming.ppt

  • 2. • Exception handling allows us to manage run-time errors in an orderly fashion • program can automatically invoke an error-handling routine when an error occurs. • Advantage: – Automation of error-handling code
  • 3. Exception Handling Fundamentals • three basic keywords: try, catch, and throw. • program statements that you want to monitor for exceptions are contained in a try block. • If an exception (i.e., an error) occurs within the try block, it is thrown (using throw). • Functions called from within a try block may also throw an exception. • The exception is caught, using catch, and processed.
  • 4. Syntax try { // try block } catch (type1 arg) { // catch block } catch (type2 arg) { // catch block } catch (type3 arg) { // catch block } .. . catch (typeN arg) { // catch block } The syntax for throw statement is : throw exception
  • 5. Handling Exceptions • When an exception is thrown, it is caught by its corresponding catch statement. • There can be more than one catch statement associated with a try. • If the data type specified by a catch matches that of the exception, then that catch statement is executed (and all others are bypassed).
  • 6. Unhandled Exceptions • If there is no applicable catch statement, an abnormal program termination may occur. • For an unhandled exception, standard library function terminate() is invoked. • By default, terminate() calls abort() to stop the program
  • 7. Example: #include <iostream> using namespace std; int main() { cout << "Startn"; try { // start a try block cout << "Inside try blockn"; throw 100; // throw an error cout << "This will not execute"; } catch (int i) { // catch an error cout << "Caught an exception -- value is: "; cout << i << "n"; } cout << "End"; } • Output: Start Inside try block Caught an exception -- value is: 100 End
  • 8. • Once an exception has been thrown, control passes to the catch expression and the try block is terminated. • catch is not called. Rather, program execution is transferred to it. • The program's stack is automatically reset. • The statements following the throw will never execute. • If the error can be fixed, execution will continue after catch, else a catch block will terminate the program with a call to exit() or abort() .
  • 9. The type of the exception must match the type specified in a catch statement The exception will not be caught and abnormal termination will occur #include <iostream> using namespace std; int main() { cout << "Startn"; try { // start a try block cout << "Inside try blockn"; throw 100; // throw an error cout << "This will not execute"; } catch (double i) { // won't work for an int exception cout << "Caught an exception -- value is: "; cout << i << "n"; } cout << "End"; return 0; } Output Start Inside try block Abnormal program termination
  • 10. Throwing an exception from a function outside the try block. #include <iostream> using namespace std; void Xtest(int test) { cout << "Inside Xtest, test is: " << test << " n"; if(test) throw test; } int main() { cout << "Startn"; try { // start a try block cout << "Inside try blockn"; Xtest(0); Xtest(1); Xtest(2); } catch (int i) { // catch an error cout << "Caught an exception -- value is: "; cout << i << "n"; } cout << "End"; return 0; } Output: Start Inside try block Inside Xtest, test is: 0 Inside Xtest, test is: 1 Caught an exception -- value is: 1 End
  • 11. Local try block #include <iostream> using namespace std; // Localize a try/catch to a function. void Xhandler(int test) { try{ if(test) throw test; } catch(int i) { cout << "Caught Exception #: " << i << 'n'; } } int main() { cout << "Startn"; Xhandler(1); Xhandler(2); Xhandler(0); Xhandler(3); cout << "End"; return 0; } Output: Start Caught Exception #: 1 Caught Exception #: 2 Caught Exception #: 3 End
  • 12. Catching Class Types • Generally an exception can be of any type. • In real-world programs, most exceptions will be class types rather than built-in types. • common reason - to create an object that describes the error. • It can be used by the exception handler to help it process the error.
  • 13. // Catching class type exceptions. #include <iostream> #include <cstring> using namespace std; class MyException { public: char str_what[80]; int what; MyException() { *str_what = 0; what = 0; } MyException(char *s, int e) { strcpy(str_what, s); what = e; } }; int main() { int i; try { cout << "Enter a positive number: "; cin >> i; if(i<0) throw MyException("Not Positive", i); } catch (MyException e) { // catch an error cout << e.str_what << ": "; cout << e.what << "n"; } return 0; } Output: Enter a positive number: -4 Not Positive: -4
  • 14. Using Multiple catch Statements • There can be more than one catch associated with a try. • Each catch must catch a different type of exception.
  • 15. #include <iostream> using namespace std; // Different types of exceptions can be caught. void Xhandler(int test) { try{ if(test) throw test; else throw "Value is zero"; } catch(int i) { cout << "Caught Exception #: " << i << 'n'; } catch(const char *str) { cout << "Caught a string: "; cout << str << 'n'; } } int main() { cout << "Startn"; Xhandler(1); Xhandler(2); Xhandler(0); Xhandler(3); cout << "End"; return 0; } Output: Start Caught Exception #: 1 Caught Exception #: 2 Caught a string: Value is zero Caught Exception #: 3 End
  • 16. Handling Derived-Class Exceptions • order of catch statements is most important while inheriting. • because a catch clause for a base class will also match any class derived from that base. • to catch exceptions of both a base class and a derived class, put the derived class first in the catch sequence.
  • 17. // Catching derived classes. #include <iostream> using namespace std; class B { }; class D: public B { }; int main() { D derived; try { throw derived; } catch(B b) { cout << "Caught a base class.n"; } catch(D d) { cout << "This won't execute.n"; } return 0; } • In this example the catch clause of the base class will always be executed. • reverse the order of the catch clauses
  • 18. Exception Handling Options Additional features and nuances that make it easier and more convenient to use are, • Catching All Exceptions • Restricting Exceptions • Rethrowing an Exception
  • 19. Catching All Exceptions • This catch block will catch all kind of exceptions • Syntax: catch(...) { // process all exceptions }
  • 20. // This example catches all exceptions. #include <iostream> using namespace std; void Xhandler(int test) { try{ if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw 123.23; // throw double } catch(...) { // catch all exceptions cout << "Caught One!n"; } } int main() { cout << "Startn"; Xhandler(0); Xhandler(1); Xhandler(2); cout << "End"; return 0; } Output: Start Caught One! Caught One! Caught One! End
  • 21. This block can be made the last catch block to catch all the left out exceptions // This example uses catch(...) as a default. #include <iostream> using namespace std; void Xhandler(int test) { try{ if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw 123.23; // throw double } catch(int i) { // catch an int exception cout << "Caught an integern"; } catch(...) { // catch all other exceptions cout << "Caught One!n"; } } int main() { cout << "Startn"; Xhandler(0); Xhandler(1); Xhandler(2); cout << "End"; return 0; } OUTPUT: Start Caught an integer Caught One! Caught One! End
  • 22. Restricting Exceptions • Exceptions thrown outside the functions can be restricted. • To accomplish these restrictions, you must add a throw clause to a function definition.
  • 23. • Syntax: ret-type func-name(arg-list) throw(type-list) { //.. } • Only the types specified in the type-list can be thrown as exceptions. • Throwing any other type of expression will cause abnormal program termination • Attempting to throw an exception that is not supported by a function will cause the standard library function unexpected() to be called unexpected() -> abort()
  • 24. // Restricting function throw types. #include <iostream> using namespace std; // This function can only throw ints, chars, and doubles. void Xhandler(int test) throw(int, char, double) { if(test==0) throw test; // throw int if(test==1) throw 'a'; // throw char if(test==2) throw 123.23; // throw double } int main() { cout << "startn"; try{ Xhandler(0); // also, try passing 1 and 2 to Xhandler() } catch(int i) { cout << "Caught an integern"; } catch(char c) { cout << "Caught charn"; } catch(double d) { cout << "Caught doublen"; } cout << "end"; return 0; }
  • 25. // This function can throw NO exceptions! void Xhandler(int test) throw() { /* The following statements no longer work. Instead, they will cause an abnormal program termination. */ if(test==0) throw test; if(test==1) throw 'a'; if(test==2) throw 123.23; }
  • 26. Rethrowing an Exception • An exception can be thrown back, by calling throw, by itself, with no exception. • the current exception to be passed on to an outer try/catch sequence. • This allows multiple handlers access to the exception.
  • 27. // Example of "rethrowing" an exception. #include <iostream> using namespace std; void Xhandler() { try { throw "hello"; // throw a char * } catch(const char *) { // catch a char * cout << "Caught char * inside Xhandlern"; throw ; // rethrow char * out of function } } int main() { cout << "Startn"; try{ Xhandler(); } catch(const char *) { cout << "Caught char * inside mainn"; } cout << "End"; return 0; } Output: Start Caught char * inside Xhandler Caught char * inside main End
  • 28. Understanding terminate() and unexpected() • These functions are supplied by the Standard C++ library • These functions require the header <exception>. • Prototypes – void terminate( ); – void unexpected( );
  • 29. • The terminate() function is called whenever program attempts to rethrow an exception when no exception was originally thrown. • Eg: Unwinding the stack because of an exception, a destructor for an object may throw an exception • terminate() is the handler of last resort when no other handlers for an exception are available. terminate() -> abort()
  • 30. • The unexpected() function is called when a function attempts to throw an exception • that is not allowed by its throw unexpected() -> terminate() .
  • 31. Setting the Terminate and Unexpected Handlers • Syntax: terminate_handler set_terminate(terminate_handler newhandler) throw(); unexpected_handler set_unexpected(unexpected_handler newhandler) throw(); Both set_terminate() and set_unexpected() require the header <exception>.
  • 32. // Set a new terminate handler. #include <iostream> #include <cstdlib> #include <exception> using namespace std; void my_Thandler() { cout << "Inside new terminate handlern"; abort(); } int main() { // set a new terminate handler set_terminate(my_Thandler); try { cout << "Inside try blockn"; throw 100; // throw an error } catch (double i) { // won't catch an int exception // ... } return 0; } Output: Inside try block Inside new terminate handler abnormal program termination
  • 33. The uncaught_exception( ) Function bool uncaught_exception( ); True -> if an exception has been thrown but not yet caught. False -> Exception is caught.
  • 34. The exception and bad_exception Classes • Exceptions thrown by C++ standard library will be object derived from the base class exception. • An object of the class bad_exception can be thrown by the unexpected handler • These classes require the header <exception.h>.
  • 35. Applying Exception Handling • Exception handling is designed to provide a structured means to handle abnormal events. • The error handler must do something rational when an error occurs.
  • 36. #include <iostream> using namespace std; void divide(double a, double b); int main() { double i, j; do { cout << "Enter numerator (0 to stop): "; cin >> i; cout << "Enter denominator: "; cin >> j; divide(i, j); } while(i != 0); return 0; } void divide(double a, double b) { try { if(!b) throw b; // check for divide-by-zero cout << "Result: " << a/b << endl; } catch (double b) { cout << "Can't divide by zero.n"; } }