Skip to content

Use exceptions over aborts#191

Merged
malaterre merged 2 commits intomalaterre:masterfrom
BRAINSia:use-exceptions-over-aborts
Jun 2, 2025
Merged

Use exceptions over aborts#191
malaterre merged 2 commits intomalaterre:masterfrom
BRAINSia:use-exceptions-over-aborts

Conversation

@hjmjohnson
Copy link
Contributor

@hjmjohnson hjmjohnson commented May 28, 2025

in C++ library code, it’s generally considered bad practice to use assert for input validation or error handling. Here’s why, and what to do instead:

⚠️ Why you should not use assert in library code

  • Stripped in release builds
    assert() is disabled if NDEBUG is defined (typical for optimized builds), so important checks may silently disappear.

  • Terminates the program
    assert calls abort() on failure — not ideal for libraries meant to be reused safely by others.

  • Not user-friendly
    Gives no chance to recover or handle the failure gracefully.

  • Violates encapsulation
    Forces application behavior based on internal library assumptions.

…istency and clarity

In C++ library code, it’s generally considered bad practice to use assert for input validation or error handling.

--Stripped in release builds

assert() is disabled if NDEBUG is defined (typical for optimized builds), so important checks may silently disappear.

--Terminates the program

assert calls abort() on failure — not ideal for libraries meant to be reused safely by others.

--Not user-friendly
Gives no chance to recover or handle the failure gracefully.

--Violates encapsulation
Forces application behavior based on internal library assumptions.
Replaced unqualified `move` with explicit `std::move` in to ensure
clarity and compliance with modern C++ standards.

Fix warning about unqualified-std-cast-call
warning: unqualified call to 'std::move' [-Wunqualified-std-cast-call]
@hjmjohnson
Copy link
Contributor Author

@malaterre Do you have comments about this change? I wanted to avoid asserts that cause applications to crash when exceptions can be caught.

@malaterre malaterre merged commit ac9ff2a into malaterre:master Jun 2, 2025
2 of 3 checks passed
@malaterre
Copy link
Owner

Thanks, will start a GDCM 3.2.x with this changeset. kudos for preparing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants