• Save
Aae oop xp_13
Upcoming SlideShare
Loading in...5
×
 

Aae oop xp_13

on

  • 466 views

 

Statistics

Views

Total Views
466
Views on SlideShare
382
Embed Views
84

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 84

http://niitcourseslides.blogspot.in 84

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Aae oop xp_13 Aae oop xp_13 Presentation Transcript

  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: #include<iostream> int main() { try {//Statements} cout << "One more statement" << endl; catch(int var) {//Statements}©NIIT OOPS/Lesson 13/Slide 20 of 28
  • 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
  • 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
  • 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
  • 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 (a>b?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 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
  • 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
  • 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
  • Exception Handling and TemplatesSummary (Contd..) There are two types of templates:  Function Template  Class Template©NIIT OOPS/Lesson 13/Slide 28 of 28