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

33 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



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?