C++ catch(std::exception & e ) vs. catch(…)

后端 未结 4 1884
傲寒
傲寒 2020-12-11 03:58

I know the difference in handling of both of these catches, but what does it take for the ellipse to catch something the std::exception catch wouldn\'t catch?

For ex

4条回答
  •  爱一瞬间的悲伤
    2020-12-11 04:34

    You probably meant:

    throw std::runtime_error("runtime error!"); // not std::runtime
    

    The std::runtime_error is derived from the std::exception so your first catch block is fired up as it catches exceptions of type std::exception. And there you probably meant:

    std::cout << "Exception: " << e.what(); // not e
    

    If you threw anything else other than the std::run_time or std::exception and its derivatives, the second catch block would be triggered. Useful reading from the C++ FAQ: What should I throw?

提交回复
热议问题