catching std::exception by reference?

后端 未结 4 1050
时光取名叫无心
时光取名叫无心 2021-02-01 05:30

I have a silly question. I read this article about std::exception http://www.cplusplus.com/doc/tutorial/exceptions/

On catch (exception& e), it says:

相关标签:
4条回答
  • 2021-02-01 06:10

    No the & has absolutely no bearing on the polymorphic nature of exception handlers. Their wording is very poor, it does seem to indicate that the & is somehow responsible. This is not the case. You are correct, & just passes by reference which is a tad more efficient.

    Also as a general rule, you should really try to avoid cplusplus.com.

    Updated link: What's wrong with cplusplus.com

    0 讨论(0)
  • 2021-02-01 06:11

    The reason for using & with exceptions is not so much polymorphism as avoiding slicing. If you were to not use &, C++ would attempt to copy the thrown exception into a newly created std::exception, potentially losing information in the process. Example:

    #include <stdexcept>
    #include <iostream>
    
    class my_exception : public std::exception {
      virtual const char *what() const throw() {
        return "Hello, world!";
      }
    };
    
    int main() {
      try {
        throw my_exception();
      } catch (std::exception e) {
        std::cout << e.what() << std::endl;
      }
      return 0;
    }
    

    This will print the default message for std::exception (in my case, St9exception) rather than Hello, world!, because the original exception object was lost by slicing. If we change that to an &:

    #include <stdexcept>
    #include <iostream>
    
    class my_exception : public std::exception {
      virtual const char *what() const throw() {
        return "Hello, world!";
      }
    };
    
    int main() {
      try {
        throw my_exception();
      } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
      }
      return 0;
    }
    

    Now we do see Hello, world!.

    0 讨论(0)
  • 2021-02-01 06:14

    Does this mean that by using "&" you can also catch exception of the parent class?

    No, this doesn't increase the scope of where you will catch exceptions from (e.g. from the parent class of the class that contains the try/catch code).

    It also doesn't increase the types of exceptions that can be caught, compared to catching by value (catch(std::exception e) without the & - you'll still catch each exception that either is std::exception or derives from it).

    What it increases is the amount of data that you will actually get when you catch the exception.

    If an exception is thrown that derives from std::exception, and you catch it by value, then you are throwing out any extra behavior in that exception class. It breaks polymorphism on the exception class, because of Slicing.

    An example:

    class MyException : public std::exception
    {
    public:
        virtual const char* what() const
        {
            return "hello, from my exception!";
        }
    };
    
    // ...
    
    try
    {
        throw MyException();
    }
    catch(std::exception& e)
    {
        // This will print "hello, from my exception!"
        std::cout << e.what() << "\n";
    }
    
    // ...
    
    try
    {
        throw MyException();
    }
    catch(std::exception e)
    {
        // This will print "Unknown exception"
        std::cout << e.what() << "\n";
    }
    
    0 讨论(0)
  • 2021-02-01 06:15

    Using reference to exception here can reduce the temporary objects created, and it can also keep the polymorphism.

    0 讨论(0)
提交回复
热议问题