1

If a c++ function has no exception, I have to write noexcept explicitly; if a c++ has exceptions, I don't have to write anything. Why not just the opposite? If a c++ function has exceptions, I have to write except explicitly; if a c++ has no exception, I don't have to write anything.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
macomphy
  • 413
  • 1
  • 9
  • `noexpect` is same as `noexpect(true)` and for potentially throwing function `noexpect(false)`, which is the same as not writing anything. – Ch3steR Jul 04 '22 at 06:23
  • 3
    Most likely because of backwards compatibility. Would you like to go through all source code to add `except` to every single function that may throw, or rather mark the ones that don’t when otherwise working on that part of code in a 100-1000kLOC codebase? – Sami Kuhmonen Jul 04 '22 at 06:26
  • 2
    The default is to make no assumptions about whether a function may throw exceptions or not. You are, however, permitted to be explicit about whether a function throws, or doesn't. – Peter Jul 04 '22 at 06:27
  • 1
    If you like this way of design, you're looking for [rust](https://doc.rust-lang.org/book/). C++ philosophy is that there is no hand-holding restrictions, but you may explicitly specify them. – Louis Go Jul 04 '22 at 06:27
  • @LouisGo It's also pretty common for people who have found to post questions asking why C++ does not support features that does, or ask why it does things differently. Seems to be a common mindset with people using other programming languages that they need to criticise or question C++ about being different to other languages. – Peter Jul 04 '22 at 06:30
  • related: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Re-noexcept – moooeeeep Jul 04 '22 at 06:35
  • Most of C++ is for historical reasons; C++ didn't have exceptions in the beginning, and `noexcept` was only added a few years ago. Requiring billions of lines of decades-old robust and working code to be modified just because you got an idea that *might* be better will not make you any friends. – molbdnilo Jul 04 '22 at 07:47
  • 1
    `noexcept` only guarantees a function doesn't throw by terminating otherwise. This can have some (albeit minimal) overhead: . (To create a call stack with the unwind instruction to terminate). This is pretty different from how some other languages handle exceptions (no pun intended) – Artyer Jul 04 '22 at 07:56

1 Answers1

2

Writing a C++ function that you are 100 % certain will not throw an exception is far from easy and requires a thorough dedicated attention.

I consider I am allowed to write this noexcept keyword as the reward.

Enlico
  • 23,259
  • 6
  • 48
  • 102
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • Well, as regards _writing_ (I assume _a new_) _function_, I could always wrap the body in `try`-`catch` and use `std::optional` as the return type. – Enlico Jul 04 '22 at 06:34
  • Sounds like you've just (partially) designed the most loved programming language on SO. ;) – Chris Jul 04 '22 at 06:36
  • 1
    @Enlico, or treat error and return value together by [`std::expected`](https://en.cppreference.com/w/cpp/header/expected) in C++23. – Louis Go Jul 04 '22 at 06:38
  • @Enlico What if the compiler analyzes your try block cannot throw, optimizes out your catch block, and then the program is terminated when an exception is thrown? https://docwiki.embarcadero.com/RADStudio/Sydney/en/Differences_Between_Clang-enhanced_C%2B%2B_Compilers_and_Previous-Generation_C%2B%2B_Compilers#Try_Blocks_Cannot_Handle_Some_Exceptions – VLL Jul 04 '22 at 06:41
  • But "100 % certain will not throw an exception" is not the real meaning of `noexcept`. The real meaning of `noexcept` is: the function will not throw any exception to caller, if there is an exception, the program will terminate. And in most of the time, caller can not handle callee's exception, exception safety is very difficult, too. So, I think function should be `noexcept` by default. – macomphy Jul 04 '22 at 06:45
  • @VLL, are you talking about hardware exceptions? (I'm ignorant in this respect, so I'm using the wording used by a high rep user in a comment under [this answer](https://stackoverflow.com/a/2350530/5825294).) – Enlico Jul 04 '22 at 06:59
  • @macomphy but that would mean that any function you didn't explicitly mark would basically be a landmine in code that used exceptions. If any non-marked function is in your callstack you'd crash your program. That would make exception handling almost useless especially when interacting with 3rd party code – PeterT Jul 04 '22 at 06:59
  • @macomphy - There is a third option for a noexcept function - if there is an exception, it will be caught and handled. And only then the function returns normally. – BoP Jul 04 '22 at 09:04