UT Dallas CS 4337 - Chapter16 Template STL (56 pages)

Previewing pages 1, 2, 3, 4, 26, 27, 28, 53, 54, 55, 56 of 56 page document View the full content.
View Full Document

Chapter16 Template STL



Previewing pages 1, 2, 3, 4, 26, 27, 28, 53, 54, 55, 56 of actual document.

View the full content.
View Full Document
View Full Document

Chapter16 Template STL

37 views


Pages:
56
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 16 Exceptions Templates and the Standard Template Library STL Copyright 2012 Pearson Education Inc 6 1 Exceptions Copyright 2012 Pearson Education Inc Division by zero Error if denominator 0 cout ERROR Cannot divide by zero n else quotient numerator denominator Copyright 2012 Pearson Education Inc Division by zero Error Double divide int numerator int denominator if denominator 0 cout ERROR Cannot divide by zero n return 0 else return static cast double numerator denominator unreliable because 0 is a valid result of a division operation 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 requires 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 signal Copyright 2012 Pearson Education Inc Division by zero Error Double divide int numerator int denominator if denominator 0 the throw point throw ERROR Cannot divide by zero n else return static cast double numerator denominator 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 Exceptions Flow of Control 1 2 3 A function that throws an exception is called from within a try block 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 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 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 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 Exceptions What Happens 1 try block is entered totalDays function is called 2 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 executes Copyright 2012 Pearson Education Inc From Program 16 1 Copyright 2012 Pearson Education Inc From Program 16 1 Copyright 2012 Pearson Education Inc What Happens in theTry Catch Construct Copyright 2012 Pearson Education Inc What if no exception is thrown 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 caught 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 terminate 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 class Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Contents of Rectangle h Version1 Continued Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Program 16 2 Continued Copyright 2012 Pearson Education Inc Extracting Data from the Exception Class Exception object passes data back to the exception handler See Rectangle Version 3 Copyright 2012 Pearson Education Inc What Happens After catch Block Once an exception is thrown the program cannot return to throw point The function executing throw terminates does not return other calling functions in try block terminate resulting in unwinding the stack If objects were created in the try block and an exception is thrown they are destroyed Copyright 2012 Pearson Education Inc Nested try Blocks try catch blocks can occur within an enclosing try block Exceptions caught at an inner level can be passed up to a catch block at an outer level catch throw pass exception up to next level Copyright 2012 Pearson Education Inc Bad alloc Exception When the new operator fails to allocate memory an exception is thrown This is called bad alloc exception Defined in the new header file include new try code that uses the new operator catch bad alloc code that responds to the error See Pr16 6 cpp Copyright 2012 Pearson Education Inc 16 2 Function Templates Copyright 2012 Pearson Education Inc Function Templates Function template a pattern for a function that can work with many data types When written parameters are left for the data types When called compiler generates code for specific data types in function call Copyright 2012 Pearson Education Inc Function Template Example template prefix template class T T times10 T num return 10 num generic data type type parameter What gets generated when times10 is called with an int What gets generated when times10 is called with a double int times10 int num return 10 num double times10 double num return 10 num Copyright 2012 Pearson Education Inc Function Template Example template class T T times10 T num return 10 num Call a template function in the usual manner int ival 3 double dval 2 55 cout times10 ival displays 30 cout times10 dval displays 25 5 See Pr16 8 cpp Copyright 2012 Pearson Education Inc Function Template Notes Can define a template to use multiple data types


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

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 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?