Exception Handling and TemplatesObjectivesIn this lesson, you will learn to: Handle runtime errors in a program Create and...
Exception Handling and TemplatesException Handling An exception can be defined as an unexpected event  that occurs during ...
Exception Handling and TemplatesWhen to Use Exceptions There can be three types of outcomes when a  function is called dur...
Exception Handling and TemplatesProblem Statement 13.D.1    #include iostream    #include cstring    class String    {    ...
Exception Handling and TemplatesProblem Statement 13.D.1 (Contd..)    String(char *inString)        {str = new char[strlen...
Exception Handling and TemplatesProblem Statement 13.D.1 (Contd..)    }    void display()    {cout  str;         }};    in...
Exception Handling and TemplatesProblem Statement 13.D.1 (Contd..) The above code generates the following runtime error:  ...
Exception Handling and TemplatesException Handling Exception-handling is a feature of C++ that provides a  standard facili...
Exception Handling and TemplatesExecution of try, catch and throw Statements  (Contd..) The control reaches the try statem...
Exception Handling and TemplatesException Handling (Contd..) If a matching catch handler is found, its parameter is  initi...
Exception Handling and Templates  Problem Statement 13.D.2  Make appropriate changes in the following program.  So that ex...
Exception Handling and TemplatesProblem Statement 13.D.2 (Contd..)    String(char *inString)    {str = new char[strlen(inS...
Exception Handling and TemplatesProblem Statement 13.D.2 (Contd..)    void display()    {cout  str;    }};    int main()  ...
Exception Handling and TemplatesUnHandled Exceptions If an exception is not caught by any catch  statement, then special f...
Exception Handling and TemplatesProblem Statement 13.P.1 To avoid the incorrect use of the Customer class, the  class need...
Exception Handling and TemplatesProblem Statement 13.P.1 (Contd..)PhoneNo[11]AmountOutstandingGet()Print()Validate()Hint:T...
Exception Handling and TemplatesTypes of Exceptions Synchronous Exceptions- Synchronous exceptions  are those that occur d...
Exception Handling and TemplatesTips on Exception-Handling A try block must be followed by a set of catch  statements. Onc...
Exception Handling and TemplatesTips on Exception-Handling (Contd..) The statements in the catch-handler determine  whethe...
Exception Handling and TemplatesJust a Minute…2. The _____, ______, and ________ keywords are   associated with exception-...
Exception Handling and TemplatesJust a Minute…(Contd..)    catch(...)        {           //Statements }        return 0;  ...
Exception Handling and TemplatesTemplates Templates provide generic functionality. Templates can be categorized into two c...
Exception Handling and TemplatesFunction Template They allow to create generic functions They admit any data type as param...
Exception Handling and TemplatesFunction Template (Contd..) The syntax to create a template function is as  follows:  temp...
Exception Handling and TemplatesClass Template A class can have members based on generic types  means their data type is n...
Exception Handling and TemplatesSummaryIn this lesson, you learned that: Exceptions are erroneous events or anomalies  occ...
Exception Handling and TemplatesSummary (Contd..) Each catch‑handler is associated with a throw  expression and performs t...
Exception Handling and TemplatesSummary (Contd..) There are two types of templates:     Function Template     Class Templa...
Upcoming SlideShare
Loading in …5
×

Aae oop xp_13

476 views
450 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
476
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Aae oop xp_13

  1. 1. Exception Handling and TemplatesObjectivesIn 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. 2. Exception Handling and TemplatesException 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. 3. Exception Handling and TemplatesWhen 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 programs control influence the outcome of a function.©NIIT OOPS/Lesson 13/Slide 3 of 28
  4. 4. Exception Handling and TemplatesProblem 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. 5. Exception Handling and TemplatesProblem 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. 6. Exception Handling and TemplatesProblem 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. 7. Exception Handling and TemplatesProblem 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. 8. Exception Handling and TemplatesException 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. 9. Exception Handling and TemplatesExecution 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. 10. Exception Handling and TemplatesException 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. 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. 12. Exception Handling and TemplatesProblem 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. 13. Exception Handling and TemplatesProblem 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. 14. Exception Handling and TemplatesUnHandled 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. 15. Exception Handling and TemplatesProblem 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. 16. Exception Handling and TemplatesProblem Statement 13.P.1 (Contd..)PhoneNo[11]AmountOutstandingGet()Print()Validate()Hint:The validate() function should throw an exception ifthe data is incorrect.©NIIT OOPS/Lesson 13/Slide 16 of 28
  17. 17. Exception Handling and TemplatesTypes 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. 18. Exception Handling and TemplatesTips 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. 19. Exception Handling and TemplatesTips 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. 20. Exception Handling and TemplatesJust 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. 21. Exception Handling and TemplatesJust 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. 22. Exception Handling and TemplatesTemplates Templates provide generic functionality. Templates can be categorized into two categories: Function Template Class Template©NIIT OOPS/Lesson 13/Slide 22 of 28
  23. 23. Exception Handling and TemplatesFunction 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. 24. Exception Handling and TemplatesFunction 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. 25. Exception Handling and TemplatesClass 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. 26. Exception Handling and TemplatesSummaryIn 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. 27. Exception Handling and TemplatesSummary (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. 28. Exception Handling and TemplatesSummary (Contd..) There are two types of templates: Function Template Class Template©NIIT OOPS/Lesson 13/Slide 28 of 28

×