ECE 150 Fundamentals of Programming Throwing exceptions Prof

  • Slides: 15
Download presentation
ECE 150 Fundamentals of Programming Throwing exceptions Prof. Hiren Patel, Ph. D. Douglas Wilhelm

ECE 150 Fundamentals of Programming Throwing exceptions Prof. Hiren Patel, Ph. D. Douglas Wilhelm Harder, M. Math. LEL hdpatel@uwaterloo. ca dwharder@uwaterloo. ca © © 2018 by by Douglas Wilhelm Harder and Hiren Patel.

Throwingexceptions Outline • In this lesson, we will: – Describe the concept of throwing

Throwingexceptions Outline • In this lesson, we will: – Describe the concept of throwing exceptions – Introduce exception classes and the stdexcept library • A common constructor and a public member function what() – Look at examples of throwing exceptions

Throwingexceptions Constructors • Suppose something goes wrong – We can throw an exception that

Throwingexceptions Constructors • Suppose something goes wrong – We can throw an exception that raises • The Standard Template Library (STL) defines a number of exception classes, all of which have a similar interface: – The constructor takes a string as an argument – A public member function what() returns that string as a character array

Throwingexceptions Thrown exceptions • The default behavior for thrown exceptions is program termination: –

Throwingexceptions Thrown exceptions • The default behavior for thrown exceptions is program termination: – Here, the array length is invalid: #include <iostream> int main(); int main() { int n{-10}; int *a_data = new int[n]; delete[] a_data; return 0; } terminate called after throwing an instance of 'std: : bad_array_new_length' what(): std: : bad_array_new_length Abort (core dumped)

Throwingexceptions Standard exceptions • The library stdexcept defines nine standard exceptions – These are

Throwingexceptions Standard exceptions • The library stdexcept defines nine standard exceptions – These are related to design issues: Class Description Example logic_error An issue that should have been dealt with in development The conditions are not met for calling a function domain_error A function is being called with an invalid argument Calling a logarithm on a negative number invalid_argume nt A more general domain error An invalid u. Waterloo Student ID number An invalid length an array Making an array holding a string shorter than the string length_error out_of_range An array or string index is Accessing a character invalid beyond the end of a You don’t have to memorize these string – for informational purposes only

Throwingexceptions Standard exceptions • The library stdexcept defines nine standard exceptions – These are

Throwingexceptions Standard exceptions • The library stdexcept defines nine standard exceptions – These are related to run-time issues: Class Description Example runtime_error An issue that can only be detected during execution The conditions are not met for calling a function range_error The result of a function is not representable Calling a logarithm on a negative number An arithmetic operation results in an overflow Adding two ints with a result greater than or equal to 231 overflow_error underflow_erro r An arithmetic operation results in an underflow Taking the difference between two unsigned int with the result being negative You don’t have to memorize these – for informational purposes only

Throwingexceptions Thrown exceptions • By definition, the argument of a factorial cannot be negative,

Throwingexceptions Thrown exceptions • By definition, the argument of a factorial cannot be negative, but not all factorials can be represented by int #include <stdexcept> int factorial( int n ) { if ( n < 0 ) { throw std: : domain_error( "Cannot calculate n! for n < 0. " ); } else if ( n <= 12 ) { int result{1}; for ( int k{1}; k <= n; ++k ) { result *= k; } return result; } else { // This could also be a range error throw std: : overflow_error( "n! cannot be represented as an 'int'. " ); } }

Throwingexceptions Thrown exceptions • For example, if we execute the following code: #include <stdexcept>

Throwingexceptions Thrown exceptions • For example, if we execute the following code: #include <stdexcept> #include <iostream> // Function declarations int main(); int factorial( int n ); int main() { std: : cout << << factorial( 10 ) << std: : endl; factorial( -5 ) << std: : endl; factorial( 99 ) << std: : endl; "Finished execution. " << std: : endl; return 0; } Output: 3628800 terminate called after throwing an instance of 'std: : domain_error' what(): Cannot calculate n! for n < 0. Aborted (core dumped)

Throwingexceptions Thrown exceptions • If we comment out the first error, the second terminates

Throwingexceptions Thrown exceptions • If we comment out the first error, the second terminates execution #include <stdexcept> #include <iostream> // Function declarations int main(); int factorial( int n ); int main() { std: : cout << factorial( 10 ) << std: : endl; // std: : cout << factorial( -5 ) << std: : endl; std: : cout << factorial( 99 ) << std: : endl; std: : cout << "Finished execution. " << std: : endl; return 0; } Output: 3628800 terminate called after throwing an instance of 'std: : overflow_error' what(): n! cannot be represented as an 'int'. Aborted (core dumped)

Throwingexceptions Thrown exceptions • Our rational number class can also now throw exceptions: Rational:

Throwingexceptions Thrown exceptions • Our rational number class can also now throw exceptions: Rational: : operator*( int n ) { int factor{ gcd( n, denominator_ ) }; n /= factor; if ( (n*numerator_)/n != numerator_ ) { throw overflow_error( "n * a/b is not representable with 'int'" ); } return Rational( n*numerator_, denominator_/factor ); } 1

Throwingexceptions Looking ahead • In addition to throwing instances of these classes, we will:

Throwingexceptions Looking ahead • In addition to throwing instances of these classes, we will: – See how to catch these exceptions • A form of flow control between functions – Describe how some of these classes are related through inheritance 1

Throwingexceptions Summary • Following this lesson, you now – Understand the concept of throwing

Throwingexceptions Summary • Following this lesson, you now – Understand the concept of throwing exceptions – Are aware of the various classes of exceptions • They have a common interface – Know how to generate and throw these exceptions 1

Throwingexceptions References [1] cplus. com http: //www. cplus. com/reference/stdexcept/ 1

Throwingexceptions References [1] cplus. com http: //www. cplus. com/reference/stdexcept/ 1

Throwingexceptions Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times

Throwingexceptions Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https: //www. rbg. ca/ for more information. 1

Throwingexceptions Disclaimer These slides are provided for the ECE 150 Fundamentals of Programming course

Throwingexceptions Disclaimer These slides are provided for the ECE 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended. 1