-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Want to know exception type when an exception is thrown. #539
Comments
Warning: Code untested. AFAIK "std::exception" is the default return value for what() of an
Having a exception message is always a good idea. Printing the type name from within Catch could be done using There are different demangle function, which you can use depending on your target compiler and used libraries. If you have Boost in your code, you could use that to auto-generate the what() text based on the current exception:
That could be reused if you had a parent exception class for all of your exceptions. |
Kinky ( ͡o ͜ʖ ͡o) |
Describing that as the "default" return value is somewhat misleading - it just happens to be what some implementations happen to return (others do different things, e.g. an empty string). The standard describes |
Catch has the ability to translate custom exception types (even those that do not derive from std::exception) into strings. I've just fixed both those things (in v1.3.0-develop.3)! |
Great, if all that functionality is already available, couldn't we have a default output like
|
It does! Or is there something else it's not doing that you're missing? |
Sorry, lost track of this for a bit. Thanks Phil ( @philsquared ). I may play with that functionality at some point, but defining behaviour on a per-exception basis doesn't help me if I'm unsure of what exception is being thrown. In the end, I took the advice of @webmaster128 to overrode the what() method in my exceptions. It means they take up much more real estate in my files, and more effort to write, but it does lead to better code. Writing specialized code to print something useful in unit tests didn't seem to be a good use of the effort. But really, thanks so much Phil. Again, I really love Catch. @webmaster128, Thanks for the advice. Also, I believe your override of what() is missing a noexcept specifier. |
@philsquared What you are missing is that Catch does not print the exception type at all. In any case, only the output of what() is shown. I'd consider it a an unexpected behavior that GCC's std::exception puts it's name in the what(). According to http://en.cppreference.com/w/cpp/error/exception/exception the what() should be empty. As you can see in the following output, no exception name is printed (source below).
|
It's not unexpected - but neither is it expected.. that just happens to be what the version of GCC that you're using today happens to have done today. A different version of GCC, or a different compiler, or another day, may all behave differently.
cppreference is mistaken. As I've mentioned previously, the standard says:
Which may (or may not) be empty. It may (or may not) be "std::exception". Or it may be "no exception was thrown". Or anything else, really (as long as the implementation defines what it is) - the only thing we can say with any certainty is that it's some null-terminated byte sequence. |
Thanks @PureAbstract for going into detail. The only point that matters here in case of Catch is that the exception name is not printed, even if it looks like that at first glance. |
Did the stringification ability mentioned above make it to Catch2? I just ended up chasing down a Boost exception (from It would be really nice to have the exception type printed - heck, I'd even take a mangled version, although I realize that's probably not something you want in an actual release. |
After further investigation, I discovered
Will print the demangled type along with the exception for exceptions that inherit from It would still be nice to have exception names by default, but this is a good clean workaround for anyone who wants it now! |
It seems to me that such an exception:
is actually not quite right. You should allow for a message and use I have exceptions defined in this way:
And when a number is too big:
Then you already have a message and the |
Thanks in advance. I love Catch.
I generally try to make numerous exceptions with useful names such as
NumberIsWayTooBig()
. These inherit directly from std::exception:When such an exception is thrown in a Catch test, I get a message:
I then have to write the test in a non-Catch test file to get the type of the exception object as I would normally expect it:
Is there any way to get the type of the particular exception object?
The text was updated successfully, but these errors were encountered: