SlideShare a Scribd company logo
Exception Handlers One exception handler for every type of exception that we want to catch. Exception handlers immediately follow the try block & are denoted by the keyword catch, e.g. try { // code that may generate exceptions } catch(type1 id1) { // handle exceptions of type1 } catch(type2 id2) { // handle exceptions of type2 }
Exception Handlers Each catch clause takes a  single argument  of one particular type. The identifier id1,id2 may be used inside the handler. The identifier is not necessary. Handlers must appear directly after try block with no other code in between . Handler searched like the switch statement except for the break keyword.
Exception Specification Informs the user of a function about the type of exceptions the function can throw. Is a part of  function declaration  appearing after the argument list of the function. Uses keyword  throw  followed by parenthesized list of all potential exception types.
Exception specification contd. void f() throw(toobig, toosmall, divzero); Function can throw exceptions of type toobig, toosmall, divzero only. void f(); Any type of exception may be thrown from the function. void f() throw(); No exceptions are thrown from a function.
unexpected() Called when you throw something other than what appears in the exception specification. Implemented with a pointer to a function. By default,  unexpected  calls  terminate . We can change its behavior using the  set_unexpected()  function which takes the address of a function with no arguments and void   return value. Returns previous value of the  unexpected( )  pointer which can be restored later. Header file <exception>
set_unexpected() e.g void my_unexpected(){ cout << &quot;unexpected exception thrown&quot;; exit(1); } int main() { //set our function handler for unexpected exceptions set_unexpected(my_unexpected); //code that raises an unexpected exception }
Uncaught exceptions If none of the exception handlers following a particular  try  block matches an exception,then Exception moves to the next-higher context, i.e. the function/ try   block surrounding the  try   block that failed to catch the exception. This process continues until, at some level, a handler matches the exception. At that point, the exception is considered “caught” and no further searching occurs.
terminate( ) terminate()  is automatically called if an exception is uncaught. Is actually a pointer to a function. Default value is the Standard C library function  abort( ) . No cleanups occur for an uncaught exception
set_terminate( ) We can install our own  terminate()  function using the  set_terminate()  function. Returns a pointer to the previous  terminate()  function. Custom  terminate()  takes no arguments and returns a void value. Any  terminate( )   handler installed must not return or throw an exception, but instead must call some sort of program-termination function.
set_terminate( ) e.g. void terminator() { cout << “Custom terimate ftn&quot; << endl; abort(); } void (*old_terminate)() = set_terminate(terminator);
set_terminate( ) e.g. #include <eh.h> #include <process.h> #include <iostream.h> void term_func(){ cout << &quot;term_func() was called by terminate().\n&quot;; // ... cleanup tasks performed here // If this function does not exit, abort is called. exit(-1); } void main(){ int i = 10, j = 0, result; set_terminate( term_func ); try{ if( j == 0 )  throw &quot;Divide by zero!&quot;; else   result = i/j; } catch( int ){ cout << &quot;Caught some integer exception.\n&quot;; } cout << &quot;This should never print.\n&quot;; }
Catching any Exception If a function has no exception specification,  any  type of exception can be thrown. Solution is to create a handler that catches any exception. Done by using ellipses in the argument list. catch(...) { cout << “An exception was thrown&quot; << endl; } Should be put at the end of exception handlers.
Rethrowing an exception Sometimes we would want to rethrow the exception that was just caught. Particularly when we use the ellipses to catch any exception because there is no information available about the exception. Accomplished by saying  throw   with no argument. catch(...) { cout << &quot;an exception was thrown&quot; << endl; throw; } Further  catch  clauses for the same  try  block are still ignored
Cleaning Up All objects in that scope  whose constructors have been completed  will have destructors called. If an exception is thrown before a constructor is completed, the associated destructor will not be called for that object.
Cleaning Up (e.g) class Cat { public: Cat() { cout << &quot;Cat()&quot; << endl; } ~Cat() { cout << &quot;~Cat()&quot; << endl; } }; class Dog { public: void* operator new(size_t sz) { cout << &quot;allocating a Dog&quot; << endl; throw int(47); } void operator delete(void* p) { cout << &quot;deallocating a Dog&quot; << endl; ::delete p; } };
Cleaning Up (e.g) contd. class UseResources { Cat* bp; Dog* op; public: UseResources(int count = 1) { cout << &quot;UseResources()&quot; << endl; bp = new Cat[count]; op = new Dog; } ~UseResources() { cout << &quot;~UseResources()&quot; << endl; delete []bp; // Array delete delete op; } };
Cleaning Up (e.g) contd. int main() { try { UseResources ur(3); } catch(int) { cout << &quot;inside handler&quot; << endl; } }  The output is the following UseResources() Cat() Cat() Cat() allocating a Dog inside handler
Solution Is to place allocations inside their own objects with their own constructors and destructors. Each allocation becomes atomic. If it fails, the other resource allocation objects are properly cleaned up. Templates offer a solution.
Solution using templates template<class T, int sz = 1> class PWrap { T* ptr; public: PWrap() { ptr = new T[sz]; cout << &quot;PWrap constructor&quot; << endl; } ~PWrap() { delete []ptr; cout << &quot;PWrap destructor&quot; << endl; } T& operator[](int i){ if(i >= 0 && i < sz) return ptr[i];  } };
Solution using templates contd. class Cat { public: Cat() { cout << &quot;Cat()&quot; << endl; } ~Cat() { cout << &quot;~Cat()&quot; << endl; } void g() {} }; class Dog { public: void* operator new[](size_t sz) { cout << &quot;allocating an Dog&quot; << endl; throw int(47); } void operator delete[](void* p) { cout << &quot;deallocating an Dog&quot; << endl; ::delete p; } };
Solution using templates contd. class UseResources { PWrap<Cat, 3> Bonk; PWrap<Dog> Og; public: UseResources() : Bonk(), Og() { cout << &quot;UseResources()&quot; << endl; } ~UseResources() { cout << &quot;~UseResources()&quot; << endl; } void f() { Bonk[1].g(); } };
Solution using templates contd. int main() { try  { UseResources ur; } catch(int) { cout << &quot;inside handler&quot; << endl; }catch(...) { cout << &quot;inside catch(...)&quot; << endl; } }
Solution using templates contd. Cat() Cat() Cat() PWrap constructor allocating a Dog ~Cat() ~Cat() ~Cat() PWrap destructor inside handler
Exception Matching Exception-handling system looks through the “nearest” handlers in the order they are written. When it finds a match, the exception is considered handled, and no further searching occurs. Matching an exception doesn’t require a perfect match between the exception and its handler. An object or reference to a derived-class object will match a handler for the base class. If handler is for an object rather than a reference, the exception object is “sliced” as it is passed to the handler.
Exception Matching class X { public: class Trouble {}; class Small : public Trouble {}; class Big : public Trouble {}; void f() { throw Big(); } };
Exception Matching int main() { X x; try { x.f(); }  catch(X::Trouble) { cout << &quot;caught Trouble&quot; << endl; }  catch(X::Small) { // Hidden by previous handler: cout << &quot;caught Small Trouble&quot; << endl; }  catch(X::Big) { cout << &quot;caught Big Trouble&quot; << endl; } }
Catch by reference not by value If you throw an object of a derived class and it is caught  by value  in a handler for an object of the base class, that object is  “sliced”  – that is, the derived-class elements are cut off and you’ll end up with the base-class object being passed. Chances are this is not what you want because the object will behave like a base-class object and not the derived class object it really is.
Catch by reference not by value class Base { public: virtual void what() { cout << &quot;Base&quot; << endl; } }; class Derived : public Base { public: void what() { cout << &quot;Derived&quot; << endl; } }; void f() { throw Derived(); }
Catch by reference not by value int main() { try { f(); } catch(Base b) { b.what(); } try { f(); } catch(Base& b) { b.what(); } } The output is Base Derived

More Related Content

What's hot (20)

PPTX
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
PDF
The Ring programming language version 1.5.4 book - Part 82 of 185
Mahmoud Samir Fayed
 
PDF
Welcome to Modern C++
Seok-joon Yun
 
PPTX
exception handling in cpp
gourav kottawar
 
PDF
Functional Programming in C#
Giorgio Zoppi
 
PPTX
Pro typescript.ch03.Object Orientation in TypeScript
Seok-joon Yun
 
PDF
PVS-Studio is there to help CERN: analysis of Geant4 project
PVS-Studio
 
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
PDF
Giorgio zoppi cpp11concurrency
Giorgio Zoppi
 
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
PDF
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
Andrey Upadyshev
 
PDF
Hot C++: New Style of Arguments Passing
Andrey Upadyshev
 
PPT
Csharp In Detail Part2
Mohamed Krar
 
DOC
What is storage class
Isha Aggarwal
 
PPT
C++ programming
viancagerone
 
PDF
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
Chris Ohk
 
PDF
AnyObject – 自分が見落としていた、基本の話
Tomohiro Kumagai
 
PPT
C tutorial
tuncay123
 
PPT
C tutorial
Khan Rahimeen
 
PPTX
C++ 11 Features
Jan Rüegg
 
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
The Ring programming language version 1.5.4 book - Part 82 of 185
Mahmoud Samir Fayed
 
Welcome to Modern C++
Seok-joon Yun
 
exception handling in cpp
gourav kottawar
 
Functional Programming in C#
Giorgio Zoppi
 
Pro typescript.ch03.Object Orientation in TypeScript
Seok-joon Yun
 
PVS-Studio is there to help CERN: analysis of Geant4 project
PVS-Studio
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
Giorgio zoppi cpp11concurrency
Giorgio Zoppi
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
Francesco Casalegno
 
[OLD VERSION, SEE DESCRIPTION FOR THE NEWER VERSION LINK] Hot С++: Universal ...
Andrey Upadyshev
 
Hot C++: New Style of Arguments Passing
Andrey Upadyshev
 
Csharp In Detail Part2
Mohamed Krar
 
What is storage class
Isha Aggarwal
 
C++ programming
viancagerone
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
Chris Ohk
 
AnyObject – 自分が見落としていた、基本の話
Tomohiro Kumagai
 
C tutorial
tuncay123
 
C tutorial
Khan Rahimeen
 
C++ 11 Features
Jan Rüegg
 

Similar to Exception Handling1 (20)

PPT
Exception handling and templates
farhan amjad
 
PPT
Handling
Amit Vats
 
PPT
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 
PDF
Exceptions ref
. .
 
PDF
10 declarative-control-flow.handouts
Andrei Alexandrescu
 
PPTX
6-Exception Handling and Templates.pptx
SatyamMishra237306
 
PPTX
Namespaces
zindadili
 
PPT
C++tutorial
dips17
 
PPT
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
DOCX
Catch and throw blocks
ashrafkhan12345
 
PPTX
Presentation on template and exception
Sajid Alee Mosavi
 
PPTX
Pre zen ta sion
Sajid Alee Mosavi
 
PDF
$Cash
ErandaMoney
 
PDF
$Cash
ErandaMoney
 
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
PPTX
C++ - UNIT_-_V.pptx which contains details about File Concepts
ANUSUYA S
 
PPT
Exception handling
zindadili
 
PPT
F6dc1 session6 c++
Mukund Trivedi
 
PPT
UNIT III.ppt
Ajit Mali
 
PPT
UNIT III (2).ppt
VGaneshKarthikeyan
 
Exception handling and templates
farhan amjad
 
Handling
Amit Vats
 
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 
Exceptions ref
. .
 
10 declarative-control-flow.handouts
Andrei Alexandrescu
 
6-Exception Handling and Templates.pptx
SatyamMishra237306
 
Namespaces
zindadili
 
C++tutorial
dips17
 
Exceptions in C++exception handling in C++, computer programming.ppt
Manwa2500
 
Catch and throw blocks
ashrafkhan12345
 
Presentation on template and exception
Sajid Alee Mosavi
 
Pre zen ta sion
Sajid Alee Mosavi
 
22 scheme OOPs with C++ BCS306B_module5.pdf
sindhus795217
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
ANUSUYA S
 
Exception handling
zindadili
 
F6dc1 session6 c++
Mukund Trivedi
 
UNIT III.ppt
Ajit Mali
 
UNIT III (2).ppt
VGaneshKarthikeyan
 
Ad

Recently uploaded (20)

PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Ad

Exception Handling1

  • 1. Exception Handlers One exception handler for every type of exception that we want to catch. Exception handlers immediately follow the try block & are denoted by the keyword catch, e.g. try { // code that may generate exceptions } catch(type1 id1) { // handle exceptions of type1 } catch(type2 id2) { // handle exceptions of type2 }
  • 2. Exception Handlers Each catch clause takes a single argument of one particular type. The identifier id1,id2 may be used inside the handler. The identifier is not necessary. Handlers must appear directly after try block with no other code in between . Handler searched like the switch statement except for the break keyword.
  • 3. Exception Specification Informs the user of a function about the type of exceptions the function can throw. Is a part of function declaration appearing after the argument list of the function. Uses keyword throw followed by parenthesized list of all potential exception types.
  • 4. Exception specification contd. void f() throw(toobig, toosmall, divzero); Function can throw exceptions of type toobig, toosmall, divzero only. void f(); Any type of exception may be thrown from the function. void f() throw(); No exceptions are thrown from a function.
  • 5. unexpected() Called when you throw something other than what appears in the exception specification. Implemented with a pointer to a function. By default, unexpected calls terminate . We can change its behavior using the set_unexpected() function which takes the address of a function with no arguments and void return value. Returns previous value of the unexpected( ) pointer which can be restored later. Header file <exception>
  • 6. set_unexpected() e.g void my_unexpected(){ cout << &quot;unexpected exception thrown&quot;; exit(1); } int main() { //set our function handler for unexpected exceptions set_unexpected(my_unexpected); //code that raises an unexpected exception }
  • 7. Uncaught exceptions If none of the exception handlers following a particular try block matches an exception,then Exception moves to the next-higher context, i.e. the function/ try block surrounding the try block that failed to catch the exception. This process continues until, at some level, a handler matches the exception. At that point, the exception is considered “caught” and no further searching occurs.
  • 8. terminate( ) terminate() is automatically called if an exception is uncaught. Is actually a pointer to a function. Default value is the Standard C library function abort( ) . No cleanups occur for an uncaught exception
  • 9. set_terminate( ) We can install our own terminate() function using the set_terminate() function. Returns a pointer to the previous terminate() function. Custom terminate() takes no arguments and returns a void value. Any terminate( ) handler installed must not return or throw an exception, but instead must call some sort of program-termination function.
  • 10. set_terminate( ) e.g. void terminator() { cout << “Custom terimate ftn&quot; << endl; abort(); } void (*old_terminate)() = set_terminate(terminator);
  • 11. set_terminate( ) e.g. #include <eh.h> #include <process.h> #include <iostream.h> void term_func(){ cout << &quot;term_func() was called by terminate().\n&quot;; // ... cleanup tasks performed here // If this function does not exit, abort is called. exit(-1); } void main(){ int i = 10, j = 0, result; set_terminate( term_func ); try{ if( j == 0 ) throw &quot;Divide by zero!&quot;; else result = i/j; } catch( int ){ cout << &quot;Caught some integer exception.\n&quot;; } cout << &quot;This should never print.\n&quot;; }
  • 12. Catching any Exception If a function has no exception specification, any type of exception can be thrown. Solution is to create a handler that catches any exception. Done by using ellipses in the argument list. catch(...) { cout << “An exception was thrown&quot; << endl; } Should be put at the end of exception handlers.
  • 13. Rethrowing an exception Sometimes we would want to rethrow the exception that was just caught. Particularly when we use the ellipses to catch any exception because there is no information available about the exception. Accomplished by saying throw with no argument. catch(...) { cout << &quot;an exception was thrown&quot; << endl; throw; } Further catch clauses for the same try block are still ignored
  • 14. Cleaning Up All objects in that scope whose constructors have been completed will have destructors called. If an exception is thrown before a constructor is completed, the associated destructor will not be called for that object.
  • 15. Cleaning Up (e.g) class Cat { public: Cat() { cout << &quot;Cat()&quot; << endl; } ~Cat() { cout << &quot;~Cat()&quot; << endl; } }; class Dog { public: void* operator new(size_t sz) { cout << &quot;allocating a Dog&quot; << endl; throw int(47); } void operator delete(void* p) { cout << &quot;deallocating a Dog&quot; << endl; ::delete p; } };
  • 16. Cleaning Up (e.g) contd. class UseResources { Cat* bp; Dog* op; public: UseResources(int count = 1) { cout << &quot;UseResources()&quot; << endl; bp = new Cat[count]; op = new Dog; } ~UseResources() { cout << &quot;~UseResources()&quot; << endl; delete []bp; // Array delete delete op; } };
  • 17. Cleaning Up (e.g) contd. int main() { try { UseResources ur(3); } catch(int) { cout << &quot;inside handler&quot; << endl; } } The output is the following UseResources() Cat() Cat() Cat() allocating a Dog inside handler
  • 18. Solution Is to place allocations inside their own objects with their own constructors and destructors. Each allocation becomes atomic. If it fails, the other resource allocation objects are properly cleaned up. Templates offer a solution.
  • 19. Solution using templates template<class T, int sz = 1> class PWrap { T* ptr; public: PWrap() { ptr = new T[sz]; cout << &quot;PWrap constructor&quot; << endl; } ~PWrap() { delete []ptr; cout << &quot;PWrap destructor&quot; << endl; } T& operator[](int i){ if(i >= 0 && i < sz) return ptr[i]; } };
  • 20. Solution using templates contd. class Cat { public: Cat() { cout << &quot;Cat()&quot; << endl; } ~Cat() { cout << &quot;~Cat()&quot; << endl; } void g() {} }; class Dog { public: void* operator new[](size_t sz) { cout << &quot;allocating an Dog&quot; << endl; throw int(47); } void operator delete[](void* p) { cout << &quot;deallocating an Dog&quot; << endl; ::delete p; } };
  • 21. Solution using templates contd. class UseResources { PWrap<Cat, 3> Bonk; PWrap<Dog> Og; public: UseResources() : Bonk(), Og() { cout << &quot;UseResources()&quot; << endl; } ~UseResources() { cout << &quot;~UseResources()&quot; << endl; } void f() { Bonk[1].g(); } };
  • 22. Solution using templates contd. int main() { try { UseResources ur; } catch(int) { cout << &quot;inside handler&quot; << endl; }catch(...) { cout << &quot;inside catch(...)&quot; << endl; } }
  • 23. Solution using templates contd. Cat() Cat() Cat() PWrap constructor allocating a Dog ~Cat() ~Cat() ~Cat() PWrap destructor inside handler
  • 24. Exception Matching Exception-handling system looks through the “nearest” handlers in the order they are written. When it finds a match, the exception is considered handled, and no further searching occurs. Matching an exception doesn’t require a perfect match between the exception and its handler. An object or reference to a derived-class object will match a handler for the base class. If handler is for an object rather than a reference, the exception object is “sliced” as it is passed to the handler.
  • 25. Exception Matching class X { public: class Trouble {}; class Small : public Trouble {}; class Big : public Trouble {}; void f() { throw Big(); } };
  • 26. Exception Matching int main() { X x; try { x.f(); } catch(X::Trouble) { cout << &quot;caught Trouble&quot; << endl; } catch(X::Small) { // Hidden by previous handler: cout << &quot;caught Small Trouble&quot; << endl; } catch(X::Big) { cout << &quot;caught Big Trouble&quot; << endl; } }
  • 27. Catch by reference not by value If you throw an object of a derived class and it is caught by value in a handler for an object of the base class, that object is “sliced” – that is, the derived-class elements are cut off and you’ll end up with the base-class object being passed. Chances are this is not what you want because the object will behave like a base-class object and not the derived class object it really is.
  • 28. Catch by reference not by value class Base { public: virtual void what() { cout << &quot;Base&quot; << endl; } }; class Derived : public Base { public: void what() { cout << &quot;Derived&quot; << endl; } }; void f() { throw Derived(); }
  • 29. Catch by reference not by value int main() { try { f(); } catch(Base b) { b.what(); } try { f(); } catch(Base& b) { b.what(); } } The output is Base Derived