I am trying to understand this program from my class. I understnad the basic for
ID: 3795311 • Letter: I
Question
I am trying to understand this program from my class. I understnad the basic form of the throw catch excption, but I don't understand what the cerr function does. It looks like it's for an output error msg of sorts? Anyways, when I run this program, it doesn't output the error msg even though p.fly is called. can someone explain this? Thanks in advance. // #include<iostream> #include<string> using namespace std; class BirdException { public: BirdException(const string s) : msg(s) {} // Does this imply that it is inhearting the msg function for this class? string msg; }; class Bird { public: virtual void fly(void) = 0; }; class Penguin : public Bird { public: virtual void fly(void) { throw BirdException("oops, penguins can't fly"); } }; int main() { Penguin p; try { p.fly(); } catch ( BirdException& be ) { cerr << "Exception caught: " << be.msg << endl; } }
Explanation / Answer
It displays the error message. I got output as below
Exception caught: oops, penguins can't fly
I am using vc++ 2013 .I checked on Unix environment, got same output
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.