-
Notifications
You must be signed in to change notification settings - Fork 55
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
Create a utility function that wraps assert but instead throws #29
Comments
Do you mean a wrapper around assert? Or do you mean an assert-like functionality that works in non-debug builds? |
I think it would be better as |
Please consider picking up ros2/rcutils#112 that got deprioritized previously. |
Possible implementation: #31 |
What is the benefit of having an assert that throws over the standard one that uses abort? Doesn't gdb break on abort just like it does with an exception? Or were you thinking of catching the exceptions? I'm just trying to understand the motivation. I do understand a |
This could be a good solution, but note that this implementation is a bit different, as in the case that the condition fails it logs to the rcutils logger (which may or may not be desired) and it uses a "DebugBreak" which is a trap, rather than throwing an exception (which it cannot do since it might be used in C only code) and rather than using If the main goal is to have an |
A benefit of throwing vs abort is that failure can be checked in unit tests. |
I see, Google Test has "Death tests" which work with asserts but I guess they're hard to use and sometimes don't work as expected on Windows: |
Also, an exception carries the additional information that the code crashed due to a |
You mean in gdb? You should be able to see the same kind of information as with an unhandled exception, but I haven't compared the two closely. The "debugbreak" style proposed in ros2/rcutils#112 might be preferable since it can be called from and across pure C99 layers too. Though I guess those cannot be as easily caught in tests as the exceptions. |
No, I meant in tests. |
I see what you mean. |
I'm thinking it can be improved by introducing an |
Issue closed in #31 |
Inspired by Kotlin's Assert, it would be helpful to have a wrapper around assert that instead throws to provide a meaningful stack trace.
The text was updated successfully, but these errors were encountered: