CS 112 - Week 13 Lecture 2 - 2022-11-17
TODAY WE WILL
* announcements
* continue intro to exception handling in C++
* [maybe an stringstream demo...?]
* prep for next class
* Should be working on Homework 9,
at-least-1st-attempts-deadline is 11:59 pm
Friday, Nov 18
* WATCH for class emails as parts of Homework 10
become available, with a deadline of DEC 2
=====
MORE on exception-handling in C++
=====
* Let's talk a bit more about the catch block.
* people DO sometimes call the thing written
in parentheses after catch in a catch block
the catch parameter...
* you can CHOOSE how the catch parameter is passed...!
* can catch by VALUE (that's the approach in exc1.cpp)
catch (out_of_range e)
* can catch by REFERENCE
catch (out_of_range& e) # might change e!
catch (const out_of_range& e) # won't change e!
* catch by POINTER (!!)
catch (out_of_range *e)
* ISO CPP's advice:
* if there's not a good reason not to,
catch by reference (!!)
* (because catch by value makes a copy --
using the copy constructor -- COULD be
situations where the copy would act
differently than the "original")
* only use catch by poointer in very
special circumstances (like when using
a library where it is the custom,
looking at you, Microsoft Foundation Class
Library...)
=====
quick examples of stringstreams!
=====
* #include <sstream> // to use stringstreams!
* if you want to use std out formatting
tools to build a string (rather than write to the
screen or to a file),
use an ostringstream
ostringstream output_ss;
output_ss << "(" << x << ", " << y << ")";
// and method str lets you get a string from
// an ostringstream
return output_ss.str();
* demo'd in revised to_string method in
Point.cpp
* and demo'd using an istringstream to more
conveniently read from a file in
iss.cpp