SlideShare a Scribd company logo
Exception Handling and Templates


Objectives
In this lesson, you will learn to:
 Handle runtime errors in a program
 Create and use templates




©NIIT                                OOPS/Lesson 13/Slide 1 of 28
Exception Handling and Templates


Exception Handling
 An exception can be defined as an unexpected event
  that occurs during the execution of a program and
  disrupts the normal flow of instructions.
 C++ reacts in the following ways when an exception
  occurs:
     The function in which the exception has occurred
      may generate a system-defined message.
     The function may terminate completely.
     The function may skip the intermediate levels and
      proceed to another section.


©NIIT                               OOPS/Lesson 13/Slide 2 of 28
Exception Handling and Templates

When to Use Exceptions
 There can be three types of outcomes when a
  function is called during program execution.
     Normal Execution- In normal execution, a function
      executes normally and returns to the calling
      program.
     Erroneous Exception- An erroneous exception
      occurs when the caller makes a mistake in passing
      arguments or calls the function out of context.
     Abnormal Execution- Abnormal execution includes
      situations in which conditions outside the
      program's control influence the outcome of a
      function.

©NIIT                               OOPS/Lesson 13/Slide 3 of 28
Exception Handling and Templates


Problem Statement 13.D.1
    #include iostream
    #include cstring
    class String
    {
        private:
        char *str;
        public:
        String()
        {str=0;}

©NIIT                       OOPS/Lesson 13/Slide 4 of 28
Exception Handling and Templates


Problem Statement 13.D.1 (Contd..)
    String(char *inString)
        {str = new char[strlen(inString)+1];
        strcpy(str,inString);
        }
        void replace(char search, char repl)
        { int counter;
            for(counter = 0; str[counter] != '0'; counter++)
        {if(str[counter] == search)
        {str[counter] = repl;}}

©NIIT                                      OOPS/Lesson 13/Slide 5 of 28
Exception Handling and Templates


Problem Statement 13.D.1 (Contd..)
    }
    void display()
    {cout  str;         }};
    int main()
    {String strObject; //The Object Does Not Contain
      Anything
        strObject.replace('+',' ');
        strObject.display();
        return 0;
    }

©NIIT                                 OOPS/Lesson 13/Slide 6 of 28
Exception Handling and Templates


Problem Statement 13.D.1 (Contd..)
 The above code generates the following runtime error:
    Segmentation fault (core dumped)




©NIIT                              OOPS/Lesson 13/Slide 7 of 28
Exception Handling and Templates


Exception Handling
 Exception-handling is a feature of C++ that provides a
  standard facility to deal with runtime exceptions.
 Exception-handling is implemented in C++ using three
  distinct components:
     Try- A demarcated block of program statements,
      known as the try block
     Catch- A set of functions known as the catch-
      handlers, which are executed when an exception
      occurs
     Throw- An expression known as throw, which
      indicates to the program to jump to the statements
      at another location, ideally an exception-handler.

©NIIT                               OOPS/Lesson 13/Slide 8 of 28
Exception Handling and Templates


Execution of try, catch and throw Statements
  (Contd..)
 The control reaches the try statement by normal
  sequential execution. The guarded section within the
  try block is executed.
 If no exception is thrown during the execution of the
  guarded section, the catch block that follow the try
  block is not executed.
 If an exception is thrown during the execution of the
  guarded section, the program searches for a catch
  clause that can handle an exception of the type
  thrown.
 If a matching handler is not found, the predefined
  runtime function, terminate(), is called.
©NIIT                               OOPS/Lesson 13/Slide 9 of 28
Exception Handling and Templates


Exception Handling (Contd..)
 If a matching catch handler is found, its parameter is
  initialized and the instructions in the catch-handler are
  executed.
 A handler for a base class will also handle exceptions
  for the classes derived from that class.




©NIIT                               OOPS/Lesson 13/Slide 10 of 28
Exception Handling and Templates


  Problem Statement 13.D.2
  Make appropriate changes in the following program.
  So that execution of the program does not halt.
    #include iostream
    #include cstring
    class String
    {private:
        char *str;
        public:
        String()
        {str=0;}

©NIIT                            OOPS/Lesson 13/Slide 11 of 28
Exception Handling and Templates


Problem Statement 13.D.2 (Contd..)
    String(char *inString)
    {str = new char[strlen(inString)+1];
        strcpy(str,inString);
    }
    void replace(char search, char repl)
    {int counter;
    for(counter = 0; str[counter] != '0'; counter++)
    {if(str[counter] == search){
    str[counter] = repl;}}}
©NIIT                              OOPS/Lesson 13/Slide 12 of 28
Exception Handling and Templates


Problem Statement 13.D.2 (Contd..)
    void display()
    {cout  str;
    }};
    int main()
    { String strObject;
    //The Object Does Not Contain Anything
    strObject.replace('+',' ');
    strObject.display();
    return 0;}
    Hint: Use try, catch block
©NIIT                             OOPS/Lesson 13/Slide 13 of 28
Exception Handling and Templates


UnHandled Exceptions
 If an exception is not caught by any catch
  statement, then special function terminate will be
  called.
 The terminate function will terminate the current
  process immediately.
 It will display an Abnormal termination error
  message.
 The syntax for terminate method is as follows:
        void terminate();



©NIIT                              OOPS/Lesson 13/Slide 14 of 28
Exception Handling and Templates


Problem Statement 13.P.1
 To avoid the incorrect use of the Customer class, the
  class needs to be modified such that an exception is
  raised if the state of the object is inappropriate. The
  structure for the Customer class is given below:
   Customer
   MobileNo[11]
   Name[25]
   DateOfBirth[9]
   BillingAddress[51]
   City[25]

©NIIT                               OOPS/Lesson 13/Slide 15 of 28
Exception Handling and Templates


Problem Statement 13.P.1 (Contd..)
PhoneNo[11]
AmountOutstanding
Get()
Print()
Validate()
Hint:The validate() function should throw an exception if
the data is incorrect.




©NIIT                              OOPS/Lesson 13/Slide 16 of 28
Exception Handling and Templates


Types of Exceptions
 Synchronous Exceptions- Synchronous exceptions
  are those that occur during runtime and can be
  generated using the throw expression.
 Asynchronous Exceptions- Asynchronous exceptions
  are those that occur due to a keyboard or mouse
  interrupt.




©NIIT                          OOPS/Lesson 13/Slide 17 of 28
Exception Handling and Templates


Tips on Exception-Handling
 A try block must be followed by a set of catch
  statements.
 Once a match is found, the subsequent catch-
  handlers are not examined.
 A thrown int value of 0 will match char *, since 0
  is converted to the NULL pointer. On the other hand,
  if a char * is thrown, it never matches an int.
 The try block followed by the catch-handlers can be
  specified within any function or at any point within the
  program.


©NIIT                               OOPS/Lesson 13/Slide 18 of 28
Exception Handling and Templates


Tips on Exception-Handling (Contd..)
 The statements in the catch-handler determine
  whether a program continues to execute after the
  handling of an exception.
 In C++, try blocks can be nested.
 If an exception is thrown before the constructor of an
  object is complete, the destructor of the object is not
  called.
 When an exception is thrown, the catch blocks are
  processed in the order in which they are written.
 When an exception is thrown, a destructor is called for
  any statically created objects that were created by the
  code up to that point in the try block.

©NIIT                               OOPS/Lesson 13/Slide 19 of 28
Exception Handling and Templates


Just a Minute…
2. The _____, ______, and ________ keywords are
   associated with exception-handling in C++.
3. Identify the error in the following code:
    #includeiostream
    int main()
    {   try
        {//Statements}
        cout  One more statement  endl;
        catch(int var)
        {//Statements}

©NIIT                                OOPS/Lesson 13/Slide 20 of 28
Exception Handling and Templates


Just a Minute…(Contd..)
    catch(...)
        {           //Statements }
        return 0;
    }
 Explain the flow of the try, catch, and throw
  statements.
 When is the terminate() function called, in the
  context of exception-handling?
 The new operator throws the ____________
  exception.

©NIIT                            OOPS/Lesson 13/Slide 21 of 28
Exception Handling and Templates


Templates
 Templates provide generic functionality.
 Templates can be categorized into two categories:
         Function Template
         Class Template




©NIIT                             OOPS/Lesson 13/Slide 22 of 28
Exception Handling and Templates


Function Template
 They allow to create generic functions
 They admit any data type as parameters
 They return a value without having to overload the
  function with all the possible data types.
 The syntax to declare the function template is as
  follows:
        template class identifier function_declaration;




©NIIT                                   OOPS/Lesson 13/Slide 23 of 28
Exception Handling and Templates


Function Template (Contd..)
 The syntax to create a template function is as
  follows:

  template class DT
  DT FindoutMax (DT a, DT b)
  {
  return (ab?a:b);
  }
 The way to call a template class with a type pattern
  is the following:
    function pattern (parameters);




©NIIT                            OOPS/Lesson 13/Slide 24 of 28
Exception Handling and Templates


Class Template
 A class can have members based on generic types
  means their data type is not defined at the time of
  creation of the class and whose members use these
  generic types.
 Consider an example to declare a class template:
    template class DT
    class pair {
        DT values [2];
    public:
        mix (DT first, DT second)
        {values[0]=first; values[1]=second;} };
©NIIT                                  OOPS/Lesson 13/Slide 25 of 28
Exception Handling and Templates


Summary
In this lesson, you learned that:
 Exceptions are erroneous events or anomalies
  occurring at runtime.
 Exception-handling is a feature of C++ provided to
  deal with errors in a standard way.
 The throw clause throws or raises an exception to
  the catch‑handler.
 The try block encloses all the program statements
  for which a throw expression exists.
 Catch‑handlers must follow the try block.

©NIIT                               OOPS/Lesson 13/Slide 26 of 28
Exception Handling and Templates


Summary (Contd..)
 Each catch‑handler is associated with a throw
  expression and performs the required action.
 There are two types of exceptions:
       Synchronous exceptions
       Asynchronous exceptions
 Synchronous exceptions are those that occur during
  runtime and can be generated using the throw
  expression.
 Asynchronous exceptions are those that occur due to
  a keyboard or mouse interrupt.
 Templates provide generic functionality.

©NIIT                             OOPS/Lesson 13/Slide 27 of 28
Exception Handling and Templates


Summary (Contd..)
 There are two types of templates:
     Function Template
     Class Template




©NIIT                             OOPS/Lesson 13/Slide 28 of 28

More Related Content

PDF
PPT
Handling Exceptions In C & C++[Part A]
PPTX
Exception handling chapter15
PDF
Exception Handling
PDF
Exception Handling in the C++ Constructor
PDF
Java unit3
PPS
Exception handling in c programming
PPT
Exception handling and templates
Handling Exceptions In C & C++[Part A]
Exception handling chapter15
Exception Handling
Exception Handling in the C++ Constructor
Java unit3
Exception handling in c programming
Exception handling and templates

What's hot (19)

PPT
Unit iii
PPT
Week7 exception handling
PPT
Handling Exceptions In C & C++ [Part B] Ver 2
PPT
Exception handling
PPTX
Exception Handling in C++
PPT
Exception handling in c++ by manoj vasava
PPT
Comp102 lec 10
DOCX
Class notes(week 8) on exception handling
PPT
Exceptions in c++
PPT
Exception handling
PDF
C++ exception handling
PDF
EXCEPTION HANDLING in C++
PPT
Exception handler
DOCX
C++ lab assignment
PPTX
exception handling in cpp
PPTX
130410107010 exception handling
PPTX
Exception handling
PPTX
Exceptions in Java
PPTX
Exceptions overview
Unit iii
Week7 exception handling
Handling Exceptions In C & C++ [Part B] Ver 2
Exception handling
Exception Handling in C++
Exception handling in c++ by manoj vasava
Comp102 lec 10
Class notes(week 8) on exception handling
Exceptions in c++
Exception handling
C++ exception handling
EXCEPTION HANDLING in C++
Exception handler
C++ lab assignment
exception handling in cpp
130410107010 exception handling
Exception handling
Exceptions in Java
Exceptions overview
Ad

Viewers also liked (7)

PPS
Dacj 4 1-a
PPTX
tL20 event handling
PPS
Dacj 2-2 b
PPS
Dacj 2-1 b
PDF
java-06inheritance
PPTX
Controls
PPS
Ajs 1 b
Dacj 4 1-a
tL20 event handling
Dacj 2-2 b
Dacj 2-1 b
java-06inheritance
Controls
Ajs 1 b
Ad

Similar to Aae oop xp_13 (20)

PDF
PPTX
Exception handling
PPTX
1.4 core programming [understand error handling]
PPTX
12. Exception Handling
PPTX
Exception Handling in C#
PPT
JP ASSIGNMENT SERIES PPT.ppt
PDF
Exceptions and Exception Handling in C++
PDF
22 scheme OOPs with C++ BCS306B_module5.pdf
PPTX
Lecture 09 Exception Handling(1 ) in c++.pptx
PDF
Oop10 6
PPT
UNIT III.ppt
PPT
UNIT III (2).ppt
PPTX
PACKAGES, INTERFACES AND EXCEPTION HANDLING
PPT
Savitch ch 16
PPTX
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
PDF
Exception handling
PDF
Exception Handling.pdf of cjfbduifhwieuJN
DOCX
MODULE5_EXCEPTION HANDLING.docx
PPT
Week7 exception handling
PPT
Week7 exception handling
Exception handling
1.4 core programming [understand error handling]
12. Exception Handling
Exception Handling in C#
JP ASSIGNMENT SERIES PPT.ppt
Exceptions and Exception Handling in C++
22 scheme OOPs with C++ BCS306B_module5.pdf
Lecture 09 Exception Handling(1 ) in c++.pptx
Oop10 6
UNIT III.ppt
UNIT III (2).ppt
PACKAGES, INTERFACES AND EXCEPTION HANDLING
Savitch ch 16
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
Exception handling
Exception Handling.pdf of cjfbduifhwieuJN
MODULE5_EXCEPTION HANDLING.docx
Week7 exception handling
Week7 exception handling

More from Niit Care (20)

PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 1-2 b
PPS
Dacj 1-3 c
PPS
Dacj 1-3 b
PPS
Dacj 1-3 a
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 1-2 b
Dacj 1-3 c
Dacj 1-3 b
Dacj 1-3 a

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
A Presentation on Artificial Intelligence
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Monthly Chronicles - July 2025
A Presentation on Artificial Intelligence
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx

Aae oop xp_13

  • 1. Exception Handling and Templates Objectives In this lesson, you will learn to: Handle runtime errors in a program Create and use templates ©NIIT OOPS/Lesson 13/Slide 1 of 28
  • 2. Exception Handling and Templates Exception Handling An exception can be defined as an unexpected event that occurs during the execution of a program and disrupts the normal flow of instructions. C++ reacts in the following ways when an exception occurs: The function in which the exception has occurred may generate a system-defined message. The function may terminate completely. The function may skip the intermediate levels and proceed to another section. ©NIIT OOPS/Lesson 13/Slide 2 of 28
  • 3. Exception Handling and Templates When to Use Exceptions There can be three types of outcomes when a function is called during program execution. Normal Execution- In normal execution, a function executes normally and returns to the calling program. Erroneous Exception- An erroneous exception occurs when the caller makes a mistake in passing arguments or calls the function out of context. Abnormal Execution- Abnormal execution includes situations in which conditions outside the program's control influence the outcome of a function. ©NIIT OOPS/Lesson 13/Slide 3 of 28
  • 4. Exception Handling and Templates Problem Statement 13.D.1 #include iostream #include cstring class String { private: char *str; public: String() {str=0;} ©NIIT OOPS/Lesson 13/Slide 4 of 28
  • 5. Exception Handling and Templates Problem Statement 13.D.1 (Contd..) String(char *inString) {str = new char[strlen(inString)+1]; strcpy(str,inString); } void replace(char search, char repl) { int counter; for(counter = 0; str[counter] != '0'; counter++) {if(str[counter] == search) {str[counter] = repl;}} ©NIIT OOPS/Lesson 13/Slide 5 of 28
  • 6. Exception Handling and Templates Problem Statement 13.D.1 (Contd..) } void display() {cout str; }}; int main() {String strObject; //The Object Does Not Contain Anything strObject.replace('+',' '); strObject.display(); return 0; } ©NIIT OOPS/Lesson 13/Slide 6 of 28
  • 7. Exception Handling and Templates Problem Statement 13.D.1 (Contd..) The above code generates the following runtime error: Segmentation fault (core dumped) ©NIIT OOPS/Lesson 13/Slide 7 of 28
  • 8. Exception Handling and Templates Exception Handling Exception-handling is a feature of C++ that provides a standard facility to deal with runtime exceptions. Exception-handling is implemented in C++ using three distinct components: Try- A demarcated block of program statements, known as the try block Catch- A set of functions known as the catch- handlers, which are executed when an exception occurs Throw- An expression known as throw, which indicates to the program to jump to the statements at another location, ideally an exception-handler. ©NIIT OOPS/Lesson 13/Slide 8 of 28
  • 9. Exception Handling and Templates Execution of try, catch and throw Statements (Contd..) The control reaches the try statement by normal sequential execution. The guarded section within the try block is executed. If no exception is thrown during the execution of the guarded section, the catch block that follow the try block is not executed. If an exception is thrown during the execution of the guarded section, the program searches for a catch clause that can handle an exception of the type thrown. If a matching handler is not found, the predefined runtime function, terminate(), is called. ©NIIT OOPS/Lesson 13/Slide 9 of 28
  • 10. Exception Handling and Templates Exception Handling (Contd..) If a matching catch handler is found, its parameter is initialized and the instructions in the catch-handler are executed. A handler for a base class will also handle exceptions for the classes derived from that class. ©NIIT OOPS/Lesson 13/Slide 10 of 28
  • 11. Exception Handling and Templates Problem Statement 13.D.2 Make appropriate changes in the following program. So that execution of the program does not halt. #include iostream #include cstring class String {private: char *str; public: String() {str=0;} ©NIIT OOPS/Lesson 13/Slide 11 of 28
  • 12. Exception Handling and Templates Problem Statement 13.D.2 (Contd..) String(char *inString) {str = new char[strlen(inString)+1]; strcpy(str,inString); } void replace(char search, char repl) {int counter; for(counter = 0; str[counter] != '0'; counter++) {if(str[counter] == search){ str[counter] = repl;}}} ©NIIT OOPS/Lesson 13/Slide 12 of 28
  • 13. Exception Handling and Templates Problem Statement 13.D.2 (Contd..) void display() {cout str; }}; int main() { String strObject; //The Object Does Not Contain Anything strObject.replace('+',' '); strObject.display(); return 0;} Hint: Use try, catch block ©NIIT OOPS/Lesson 13/Slide 13 of 28
  • 14. Exception Handling and Templates UnHandled Exceptions If an exception is not caught by any catch statement, then special function terminate will be called. The terminate function will terminate the current process immediately. It will display an Abnormal termination error message. The syntax for terminate method is as follows: void terminate(); ©NIIT OOPS/Lesson 13/Slide 14 of 28
  • 15. Exception Handling and Templates Problem Statement 13.P.1 To avoid the incorrect use of the Customer class, the class needs to be modified such that an exception is raised if the state of the object is inappropriate. The structure for the Customer class is given below: Customer MobileNo[11] Name[25] DateOfBirth[9] BillingAddress[51] City[25] ©NIIT OOPS/Lesson 13/Slide 15 of 28
  • 16. Exception Handling and Templates Problem Statement 13.P.1 (Contd..) PhoneNo[11] AmountOutstanding Get() Print() Validate() Hint:The validate() function should throw an exception if the data is incorrect. ©NIIT OOPS/Lesson 13/Slide 16 of 28
  • 17. Exception Handling and Templates Types of Exceptions Synchronous Exceptions- Synchronous exceptions are those that occur during runtime and can be generated using the throw expression. Asynchronous Exceptions- Asynchronous exceptions are those that occur due to a keyboard or mouse interrupt. ©NIIT OOPS/Lesson 13/Slide 17 of 28
  • 18. Exception Handling and Templates Tips on Exception-Handling A try block must be followed by a set of catch statements. Once a match is found, the subsequent catch- handlers are not examined. A thrown int value of 0 will match char *, since 0 is converted to the NULL pointer. On the other hand, if a char * is thrown, it never matches an int. The try block followed by the catch-handlers can be specified within any function or at any point within the program. ©NIIT OOPS/Lesson 13/Slide 18 of 28
  • 19. Exception Handling and Templates Tips on Exception-Handling (Contd..) The statements in the catch-handler determine whether a program continues to execute after the handling of an exception. In C++, try blocks can be nested. If an exception is thrown before the constructor of an object is complete, the destructor of the object is not called. When an exception is thrown, the catch blocks are processed in the order in which they are written. When an exception is thrown, a destructor is called for any statically created objects that were created by the code up to that point in the try block. ©NIIT OOPS/Lesson 13/Slide 19 of 28
  • 20. Exception Handling and Templates Just a Minute… 2. The _____, ______, and ________ keywords are associated with exception-handling in C++. 3. Identify the error in the following code: #includeiostream int main() { try {//Statements} cout One more statement endl; catch(int var) {//Statements} ©NIIT OOPS/Lesson 13/Slide 20 of 28
  • 21. Exception Handling and Templates Just a Minute…(Contd..) catch(...) { //Statements } return 0; } Explain the flow of the try, catch, and throw statements. When is the terminate() function called, in the context of exception-handling? The new operator throws the ____________ exception. ©NIIT OOPS/Lesson 13/Slide 21 of 28
  • 22. Exception Handling and Templates Templates Templates provide generic functionality. Templates can be categorized into two categories: Function Template Class Template ©NIIT OOPS/Lesson 13/Slide 22 of 28
  • 23. Exception Handling and Templates Function Template They allow to create generic functions They admit any data type as parameters They return a value without having to overload the function with all the possible data types. The syntax to declare the function template is as follows: template class identifier function_declaration; ©NIIT OOPS/Lesson 13/Slide 23 of 28
  • 24. Exception Handling and Templates Function Template (Contd..) The syntax to create a template function is as follows: template class DT DT FindoutMax (DT a, DT b) { return (ab?a:b); } The way to call a template class with a type pattern is the following: function pattern (parameters); ©NIIT OOPS/Lesson 13/Slide 24 of 28
  • 25. Exception Handling and Templates Class Template A class can have members based on generic types means their data type is not defined at the time of creation of the class and whose members use these generic types. Consider an example to declare a class template: template class DT class pair { DT values [2]; public: mix (DT first, DT second) {values[0]=first; values[1]=second;} }; ©NIIT OOPS/Lesson 13/Slide 25 of 28
  • 26. Exception Handling and Templates Summary In this lesson, you learned that: Exceptions are erroneous events or anomalies occurring at runtime. Exception-handling is a feature of C++ provided to deal with errors in a standard way. The throw clause throws or raises an exception to the catch‑handler. The try block encloses all the program statements for which a throw expression exists. Catch‑handlers must follow the try block. ©NIIT OOPS/Lesson 13/Slide 26 of 28
  • 27. Exception Handling and Templates Summary (Contd..) Each catch‑handler is associated with a throw expression and performs the required action. There are two types of exceptions: Synchronous exceptions Asynchronous exceptions Synchronous exceptions are those that occur during runtime and can be generated using the throw expression. Asynchronous exceptions are those that occur due to a keyboard or mouse interrupt. Templates provide generic functionality. ©NIIT OOPS/Lesson 13/Slide 27 of 28
  • 28. Exception Handling and Templates Summary (Contd..) There are two types of templates: Function Template Class Template ©NIIT OOPS/Lesson 13/Slide 28 of 28