DOC PREVIEW
UT Dallas CS 4337 - Chapter16 Template STL

This preview shows page 1-2-3-4-26-27-28-53-54-55-56 out of 56 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 56 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint Presentation6.1Division-by-zero ErrorSlide 4ExceptionsExceptions - TerminologySlide 7Exceptions – Key WordsExceptions – Flow of ControlHandling Division-by-zeroExceptions – Example (2)Exceptions – What HappensSlide 13Slide 14Slide 15Slide 16Exceptions - NotesException Not Caught?Exceptions and ObjectsSlide 20Slide 21Slide 22Slide 23Slide 24Slide 25Extracting Data from the Exception ClassWhat Happens After catch Block?Nested try BlocksBad_alloc Exception16.2Function TemplatesFunction Template ExampleSlide 33Function Template NotesSlide 35Slide 36Slide 3716.3Where to Start When Defining Templates16.4Class TemplatesClass Template ExampleSlide 43Class Templates and Inheritance16.5Introduction to the Standard Template LibrarySTL (Standard Template Library)Standard Template LibraryContainersIntroduction to the STL vectorDeclaring VectorsAdding Elements to a VectorRemoving Vector ElementsOther Useful Member FunctionsIteratorsAlgorithmsCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Chapter 16:Exceptions, Templates, and the Standard Template Library (STL)Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.6.1ExceptionsCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Division-by-zero Errorif (denominator == 0 )cout << “ERROR: Cannot divide by zero.\n “;elsequotient = numerator / denominator;Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Division-by-zero ErrorDouble divide(int numerator, int denominator){if (denominator == 0 ){cout << “ERROR: Cannot divide by zero.\n “;return 0;}elsereturn static_cast<double>(numerator)/ denominator; }//unreliable, because 0 is a valid result of a//division operationCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions•One way of handling error conditions •Indicate that something unexpected has occurred or been detected•Allow program to deal with the problem in a controlled manner•Can be as simple or complex as program design requiresCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions - Terminology•Exception: object or value that signals an error•Throw an exception: send a signal that an error has occurred•Catch/Handle an exception: process the exception; interpret the signalCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Division-by-zero ErrorDouble divide(int numerator, int denominator){if (denominator == 0 )// the throw pointthrow “ERROR: Cannot divide by zero.\n “;elsereturn static_cast<double>(numerator)/ denominator; }Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions – Key Words•throw – followed by an argument, is used to throw an exception–Control is passed to another part of the program known as an exception handler •try – followed by a block { }, is used to invoke code that throws an exception•catch – followed by a block { }, is used to detect and process exceptions thrown in preceding try block. Takes a parameter that matches the type thrown.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions – Flow of Control1) A function that throws an exception is called from within a try block2) If the function throws an exception, the function terminates and the try block is immediately exited. A catch block to process the exception is searched for in the source code immediately following the try block.3) If a catch block is found that matches the exception thrown, it is executed. If no catch block that matches the exception is found, the program terminates.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Handling Division-by-zero try{quotient = divide(num1, num2);cout << “The quotient is “ << quotient << endl;}Catch (string exceptionString){cout << exceptionString; }Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions – Example (2)try // block that calls function { totDays = totalDays(days, weeks); cout << "Total days: " << days; } catch (char *msg) // interpret // exception { cout << "Error: " << msg; }Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions – What Happens1) try block is entered. totalDays function is called2) If 1st parameter is between 0 and 7, total number of days is returned and catch block is skipped over (no exception thrown)3) If exception is thrown, function and try block are exited, catch blocks are scanned for 1st one that matches the data type of the thrown exception. catch block executesCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.From Program 16-1Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.From Program 16-1Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.What Happens in theTry/Catch ConstructCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.What if no exception is thrown?Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions - Notes•Predefined functions such as new may throw exceptions•The value that is thrown does not need to be used in catch block. –in this case, no name is needed in catch parameter definition–catch block parameter definition does need the type of exception being caughtCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exception Not Caught?•An exception will not be caught if–it is thrown from outside of a try block–there is no catch block that matches the data type of the thrown exception•If an exception is not caught, the program will terminateCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Exceptions and Objects•An exception class can be defined in a class and thrown as an exception by a member function•An exception class may have:–no members: used only to signal an error–members: pass error data to catch block •A class can have more than one exception classCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Contents of Rectangle.h (Version1) (Continued)Copyright © 2012 Pearson Education, Inc.Copyright


View Full Document

UT Dallas CS 4337 - Chapter16 Template STL

Download Chapter16 Template STL
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter16 Template STL and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter16 Template STL 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?