SlideShare une entreprise Scribd logo
1  sur  28
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

Contenu connexe

Tendances

Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2ppd1961
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++Deepak Tathe
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaManoj_vasava
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handlingKuntal Bhowmick
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
C++ exception handling
C++ exception handlingC++ exception handling
C++ exception handlingRay Song
 
EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++Prof Ansari
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cppgourav kottawar
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handlingHemant Chetwani
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym Lotar
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 

Tendances (19)

Unit iii
Unit iiiUnit iii
Unit iii
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2Handling Exceptions In C & C++ [Part B] Ver 2
Handling Exceptions In C & C++ [Part B] Ver 2
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
Exceptions in c++
Exceptions in c++Exceptions in c++
Exceptions in c++
 
Exception handling
Exception handlingException handling
Exception handling
 
C++ exception handling
C++ exception handlingC++ exception handling
C++ exception handling
 
EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++
 
Exception handler
Exception handler Exception handler
Exception handler
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
exception handling in cpp
exception handling in cppexception handling in cpp
exception handling in cpp
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 

En vedette

En vedette (7)

Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
 
Dacj 2-1 b
Dacj 2-1 bDacj 2-1 b
Dacj 2-1 b
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Controls
ControlsControls
Controls
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 

Similaire à Aae oop xp_13

Exception handling
Exception handlingException handling
Exception handlingWaqas Abbasi
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]tototo147
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling Intro C# Book
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJAYAPRIYAR7
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++IRJET Journal
 
Oop10 6
Oop10 6Oop10 6
Oop10 6schwaa
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.pptAjit Mali
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxWINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxssusercd11c4
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxVeerannaKotagi1
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handlingKuntal Bhowmick
 
Vb net xp_11
Vb net xp_11Vb net xp_11
Vb net xp_11Niit Care
 

Similaire à Aae oop xp_13 (20)

$Cash
$Cash$Cash
$Cash
 
Exception handling
Exception handlingException handling
Exception handling
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
 
12. Exception Handling
12. Exception Handling 12. Exception Handling
12. Exception Handling
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
JP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.pptJP ASSIGNMENT SERIES PPT.ppt
JP ASSIGNMENT SERIES PPT.ppt
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++
 
Oop10 6
Oop10 6Oop10 6
Oop10 6
 
UNIT III.ppt
UNIT III.pptUNIT III.ppt
UNIT III.ppt
 
UNIT III (2).ppt
UNIT III (2).pptUNIT III (2).ppt
UNIT III (2).ppt
 
Savitch ch 16
Savitch ch 16Savitch ch 16
Savitch ch 16
 
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptxWINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
WINSEM2016-17_CSE1002_LO_1336_24-JAN-2017_RM003_session 10.pptx
 
Exception handling
Exception handlingException handling
Exception handling
 
MODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docxMODULE5_EXCEPTION HANDLING.docx
MODULE5_EXCEPTION HANDLING.docx
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
Vb net xp_11
Vb net xp_11Vb net xp_11
Vb net xp_11
 

Plus de Niit Care (20)

Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 
Dacj 1-3 a
Dacj 1-3 aDacj 1-3 a
Dacj 1-3 a
 

Dernier

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Dernier (20)

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

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