Skip to content
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

[Improvement] Add option to remove exceptions #296

Closed
Mighty-Professional opened this issue Aug 17, 2016 · 10 comments
Closed

[Improvement] Add option to remove exceptions #296

Mighty-Professional opened this issue Aug 17, 2016 · 10 comments

Comments

@Mighty-Professional
Copy link

Mighty-Professional commented Aug 17, 2016

It would be nice to have the option to compile without exceptions for use on platforms where exceptions cannot be enabled., such as consoles etc.

Mainly for the reasons listed here: http://programmers.stackexchange.com/questions/113479/are-there-any-real-world-cases-for-c-without-exceptions

@nlohmann
Copy link
Owner

Instead of an exception, should the library just call exit() or abort?

@Mighty-Professional
Copy link
Author

Mighty-Professional commented Aug 17, 2016

Just let it crash, crash hard crash early. You could throw in asserts for more protection. This will force the user to start using the library correctly. I do not see the average person disabling exceptions so its more of a special case.

@nlohmann
Copy link
Owner

This should be done together with #244, because then we have to touch all exceptions anyway.

@nlohmann
Copy link
Owner

nlohmann commented Aug 17, 2016

Is there a more elegant way than to use preprocessor macros for this? (see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html)

@Mighty-Professional
Copy link
Author

Mighty-Professional commented Aug 17, 2016

The way that link details seems reasonable to me. At least the macro should only need to be used once with the try catch. Using -fno-exception may work with the standard library, but it will still fail to compile on any try catches or throws in your own code.

I have never done anything like that personally, I think most libraries either support them or dont. I have seen projects where you can define a preprocessor to remove exceptions though. This may cause to much extra code to be worth it.

Here is another library with the same discussion: gabime/spdlog#136. You could see how they implemented it.

@abarth
Copy link

abarth commented Sep 5, 2016

Thanks for working on this issue! I'm interested in this change to because I'd like to use your library in a project that builds with -fno-exceptions.

@abarth
Copy link

abarth commented Sep 5, 2016

Consider #define JSON_THROW(exception) std::abort() rather than defining JSON_THROW to nothing when JSON_NOEXCEPTION is set to avoid trigging control may reach end of non-void function for folks who build with [-Werror,-Wreturn-type]

nlohmann added a commit that referenced this issue Nov 13, 2016
@nlohmann
Copy link
Owner

I implemented this change. Unfortunately, the test suite cannot be compiled with -fno-exceptions (see catchorg/Catch2/issues/703). All you can do right now is

CPPFLAGS="-DJSON_NOEXCEPTION" make
test/json_unit -e

(one test fails, because an exception from std::map still occurs, but is no caught)

@nlohmann
Copy link
Owner

I shall merge this issue tomorrow.

@nlohmann
Copy link
Owner

Merged 1ab26ab. The library now respects -fno-exceptions. Additionally, exceptions can be switched off by defining JSON_NOEXCEPTION .

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

No branches or pull requests

3 participants