2

So I understand the basic concept of passing by value or reference. In this example I'm trying to catch an exception either by value or by reference:

#include <iostream>
int main() {
    try {
        throw std::invalid_argument("nope");
    } catch(std::exception &e) {
        std::cout << e.what() << std::endl;
    }
}

Outputs: "nope"

#include <iostream>
int main() {
    try {
        throw std::invalid_argument("nope");
    } catch(std::exception e) {
        std::cout << e.what() << std::endl;
    }
}

(notice the missing &) Outputs: "std::exception"

Shouldn't I get the same output given that the second version "copies" e?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
Chelz
  • 437
  • 1
  • 4
  • 9

0 Answers0