I created a custom exception class that derives from std::exception.
#include <iostream>
class Exception : std::exception {
public:
    const char* what() const noexcept override {
        return "test";
    }
};
int main() {
    try {
        throw Exception();
    } catch (std::exception& e) {
        std::cout << e.what() << std::endl;
    }
}   
This program, when compiled by g++ -stdc++=17 on Ubuntu, causes the exception to not get caught by the catch block, even though catching by reference is supposed to catch derived exceptions too. It calls std::terminate, even though it happens in a try block that catches its base class by reference. Same thing happens if Exception inherits from std::runtime_error and passes "test" to the std::runtime_error constructor in its own constructor. Normally the solution would be to only catch using Exception, but in my original code I need to catch different types of exceptions, all of which inherit from std::exception. Why does this happen? Does catching by reference to base not work? How can I catch all exceptions deriving from std::exception using one catch block?
 
     
    