How to avoid problems due to unexpected exceptions? - c++

Here's a not-too-uncommon problem in C++ programming:
Programmer A writes a C++ library (we'll call it Foo), which includes a nice public API that programmer B's program calls methods on, in order to implement part of his program. When working with the Foo API, programmer B looks at the documentation and the header file, but not very much at the .cpp files, mainly because the whole point of having a public API is to hide implementation details. Programmer B gets his program to work and initially everything looks good.
One day the program is doing its thing, when something unusual happens inside the Foo codebase that causes a Foo method to throw an exception. Programmer B was unaware that that type of exception might be thrown by that Foo method, and so the exception either doesn't get caught (and the program crashes), or the exception gets caught by a very general exception handler (e.g. catch(...)) and doesn't get handled adequately (and so the program does something non-optimal, like putting up a dialog with a cryptic error message).
Since I'd like my C++ software to be robust, my question is, what is a good way to avoid a scenario like the above? Requiring programmer B to read through all of the .cpp files in the Foo codebase (and all .cpp files of any C++ codebase that Foo calls, and so on all the way down) looking for throw statements doesn't seem like an adequate solution, since it violates the spirit of encapsulation, and even if it was done there would be no guarantee that more throw statements might not be added in the future. Requiring programmer A to document all possible thrown exceptions seems like a good idea, but in practice programmer A is very likely to miss some of them, especially if his code in turn calls into yet another library that may or may not document 100% of its possible exceptions.
In Java there is some support for automated exception-handling checks; e.g. you can annotate your Java methods to declare what exceptions they might throw, and if the calling code doesn't explicitly handle all of those exceptions (or itself declare that it might throw them), the compile fails and the programmer is forced to fix his program -- thereby preventing the problem scenario described above. C++, on the other hand, has no such compile-time enforcement, meaning that verifying that a C++ program handles all exceptions properly seems to be left entirely up to the programmer and QA team. But in a non-trivial program, actually performing such a verification seems impractical; is there some technique (automated or non-automated) by which C++ programmers can gain confidence that their programs handle all possibly-thrown exceptions in a considered fashion? (Even just a program that would list all of the exceptions that can be thrown from various calls would be useful, I think -- at least then there'd be a list of known possibilities to check the exception-handling code against)

At first take this question seems to present quite a dilemma. Until you discover the following insight.
Where exceptions are concerned, the fact that an exception is thrown, where it is thrown, and where it is caught captures more than 90% of the importance of a thrown exception's information. The type of the exception rarely is that important.
It turns out that we seldom create code that can troubleshoot a problem and come up with a remedial solution. Almost always the only useful thing code can do is report that the command failed and its error message.
If the exceptions used in a library are derived from std::exception and support a useful call to what(), then when you report/log that message, you've moved to capturing 95% of the useful information. If the what() message contains __ FILE __ , __ LINE __ , and __ func __ information, then you are at 98% of the important information provided by most thrown exceptions.
Even if a library isn't carefully documented with each exception type that can be thrown from each particular function call, a decent library will provide the base type used for all the exceptions it with throw (often it will be std::exception or something derived from it). This exception base class will have some way of getting the associated error message (such as the what() call).
But even if you are reduced to using catch (...) as your last line of defense, your error message can still indicate that a command failed and what command it was. And you have the confidence that you are handling every exception that might be thrown.

Related

What are the possible error handling strategies using Exceptions in C++, and what are their consequences and implications?

I would like your help in understanding what are the possible approaches to using/disabling exceptions in C++.
My question is not about what is the best choice but just about what are the possible options and what these options imply.
Currently, the options I can think of are:
Compiling with -fno-exceptions and giving up most std containers (possibly defining internal containers which do not throw, such as suggested in SpiderMonkey Coding_Style)
Just avoiding to throw and catch in own code, but still using std containers which may throw exceptions. Being happy with the fact that, in the case of exceptions, the program may terminate without stack unwinding, and that even RAII handled external resources may be left hanging. (This seems to be Google C++ approach according to answers to this SO question)
No using exceptions but wrapping all in a catch all std::exception try block just to make sure stack is unwound and RAII handles to external resources are released before program is terminated, as for this Cert C++ rule
As above, but also throwing exceptions which will ultimately result in program termination.
Also using catched exceptions and recovering from exceptions.
I would like to know if my understanding of options is correct, and what I might be missing out or understanding wrong.
I would also like to know whether constraints on granting basic exception safety make sense for options 2-4 (where exceptions always ultimately lead to program termination) or if/how exception safety requirement can be relaxed/restricted to specific cases (e.g. handling external resources, files).
Update
Strictly speaking, when you want to disable exceptions only compiling with no exception support is true way to go, so Option 1. Because when you want to disable exceptions you should also not use or even handle them. Raising an exception would terminate or go to a hard faulty on most implementations instantly, and only by this you would avoid overhead implications like space or performance overhead even how small (see below) they are.
However if you just want to know which paradigms about exception usage are out there, your options are almost right, with the general idea you haven't mentioned of keeping exceptions exceptional and doing things that are likely or even just could throw, at program startup.
And even more in general, in comes to error handling in general: Exceptions are there to handle errors which you come across at runtime and if done correctly errors you can ONLY detect at runtime. You can avoid exceptions by simply making sure to resolve all problems you can detect before runtime, like proper coding and review (code time), using strict types and checks (templates , compile time), and rechecking them again (static analyser).
Update 2
If you understand you wrong about caring about exception safety I would say:
Basically at first it depends whenever you enable exceptions in general: If they are disabled you can't and shouldn't care about exception safety since there are none (or if they try to come into existence, you will terminate/crash/hardfault anyway).
If you enable exceptions but don't use them in your code, like in case 2, 4 and 3, no problem you want to terminate anyway, so missing cleanup code is not relevant (and the stuff in 3. gets still run in any case). But then should make it clear to everybody that you don't want to use them, so they won't try to recover from exceptions.
And if you use them in your code, you should care about exception safety in the way, that when an exception gets thrown, you clean up after your self too, and then its up the main handler or future code changes, when ever you still terminate or can recover. Not cleaning up but still using exception wouldn't make sense. Then you can just stick to option 1.
This should be to my knowledge exhaustive. For more information see below.
Recommendation
I recommend option 4. and I tell you why:
Exceptions are a very misunderstood and misused pattern. They are misused for program flow like the languages Java does in a excessive. They are often forbidden in embedded or safety code because of their nature that is hard to tell which handler will catch it, if there is one, and how long this will take, for which the C++ std just says basically "implementation magic".
Background
However in my reasoning the hate for exceptions is basically a big XY problem:
When people are complaining that their recovery is broken and hard to tell, then the usual problem is that the don't see you can't or should do much about the most exceptions, that's what they are for. However things like a timeout or a closed tcp connection are hardly non normal, but many people use exceptions for that, that would be wrong of course. But if your whole OS tells you that there is no network adapter or no more memory what could you do? The only thing you probably want is trying to log the reason somewhere and this should be done in one try/catch around the main block.
The same is for safety/real-time programs: For the example of running out of memory, when that happens you are **** anyway, the strategy then is to do this stuff at an unsafe initialisation time, in which exceptions are no problem too.
When using containers with throwing members its a similar scenario, what would you be able to do when you would get their error code instead? Not much, thats the reason why you would make sure at code time that there is no reason for errors, like making sure that an element is really in there, or reserving capacity for your vectors.
This has the nice benefit of cleaner code, not forgetting to check for errors and no performance penalty, at least using the common C++ implementations like gcc etc.
The reason of 3. is debatable, you could do this in my opinion too, but then my question is: What do you have to do in the destructors which wouldn't cleanup your operating system anyway? The OS would cleanup memory, sockets etc.. If have a freestanding scenario, the question remains, whenever different: You plan to halt because your UART broke for example, what would you like to do before halting? And why can't you do it in the catch block before rethrow?
To sum up
Has the problem of not using any throwing code, and still be left with the problem of how to handle rare error codes. (Thats why so many C programmers still use goto or long jumps)
Not viable IMHO, worst of both
as mentioned ok, but what do you need to do in your static DTors, what you even un-normal termination wouldn't do?
My favourite
Only if you really have rare conditions that you are actual able to recover from
What I mean as a general advice: Raising and exception should mean that something happened that should never happen in normal operation, but happened due to a unforeseeable fault, like hardware fault, detached cables, non found shared libraries, a programming fault you have done, like trying to .at() at an index not in the container, which you could have known of, but the container can not. Then it is only logical that throw an exception should almost every time lead to program termination/hard fault
As a consequence of compiling with exception support, is that for example for a freestanding ARM program, your program size will increase by 27kB, probably none for hosted (Linux/Windows etc.), and 2kB RAM for the freestanding case (see C++ exception handler on gnu arm cortex m4 with freertos )
However you are paying no performance penalty for using exceptions when using common compilers like clang or gcc, when you code runs normal i.e. when your branches/ifs which would trow an exception, are not triggered.
As a reference i.e. proof to my statements I refer to ISO/IEC TR 18015:2006 Technical Report on C++ Performance, with this little excerpt from 7.2.2.3:
Enable exception handling for real-time critical programs only if
exceptions are actually used. A complete analysis must always include
the throwing of an exception, and this analysis will always be
implementation dependent. On the other hand, the requirement to act
within a deterministic time might loosen in the case of an exception
(e.g. there is no need to handle any more input from a device when a
connection has broken down). An overview of alternatives for exception
handling is given in §5.4. But as shown there, all options have their
run-time costs, and throwing exceptions might still be the best way to
deal with exceptional cases. As long as no exceptions are thrown a
long way (i.e. there are only a few nested function calls between the
throw-expression and the handler), it might even reduce run-time
costs.

which system software is responsible for Run time check

I thought this would sound a general simple question but I got up this when reading C++ exception specification. that said in one of the book, C++11 now have a keyword 'noexcept' that means no exception will be thrown from a function when it is declared with the function header and that said the reason for this keyword came into existence is C++ exception specifications are checked at run time rather than at compile time, so they offer no programmer guarantees that all exceptions have been handled. and hence they conclude two case a function would throw exception or if we are clear if it will never throw, then use noexcept for optimization(hopefully)
void foo() noexcept();
Here is the main question. Which system software perform those run time checking(I hope not compiler/linker/loader) and also which system software is responsible for allocating memory at run time(dynamic memory allocation) when this are all not taken care by compiler and others?
There is no active "system software" checking for exceptions, as you phrase it; rather, throwing an exception is an action taken by the program itself. The program passes the exception back up the stack until the exception matches an exception handler.
If no exception handler matches, then the exception is caught by the bootstrap code (main is not the actual entry point for a typical program, but is where the runtime hands control to the programmer) and the program terminates.
AFAIK this is done by the C++ runtime (libstdc++ for example). In case of exceptions, there are some guards added around the functions by the compiler (this is necessary anyway to call destructors in case exception is thrown), and in the case of noexcept, if the function throws (or if it throws other exception than advertised by the throw() specification), terminate() is called by the C++ runtime and the application is shut down.
Memory heap allocations are also (by default) done by the C++ runtime libraries.
Typically the responsible software isn't one clearly identifiable piece of code, but small fragments of code sprinkled through the executable. The compiler translates your code into binary instructions, and noexcept is no exception ;).
Indeed, you would not say that the "standard library" handles this. Exceptions and exception specifications are rather a core language feature, more fundamental than the standard library.
You could similarly ask, what piece of software ensures that when I call a function in C++, that the caller actually receives the values that I pass in? What piece of software manipulates the stack frame pointers while my program is running?
From the point of view of the standard I would say "the implementation" is responsible for these details. In some languages, like Java for instance, there is a "Java Runtime Environment" which is very clearly responsible for these things, and you could try to study exactly how it does them. In C++ there is no universal runtime environment -- like others have said, the compiler is responsible to generate code that ensures that these things happen, and that code ends up sprinkled throughout your resulting executable. How exactly the compiler achieves its task is implementation-specific, you can't give a general answer beyond what the standard says, and generally it specifies the expected behavior, not the details under the hood.
When you ask
also which system software is responsible for allocating memory at run time(dynamic memory allocation
this is again an implementation detail, it will differ from compiler to compiler.

repackaging exceptions, or keep them intact?

I'm writing some class which has 2 (main) subsystems. A part depends on boost::filesystem while another part depends on tinyxml. (Basically, it reads an xml, and depending on the data of the xml it uses boost::filesystem's functions for more access to other files).
Now both of these are "likely" to throw exceptions. I am wondering how to handle these exceptions:
The class itself -in most circumstances- can't "fix" an exception and has just to throw it back. (Most probable case would be a faulty input from the user).
However what should one do in such a case? - boost::filesystem & tinyxml both have their own exceptions, which aren't completely compatible with each other.
Should I just expect the user of this class to handle boost/tinyxml exceptions? - So far the whole usage of these libraries is hidden for the end user.
Should I repackage the extensions into my own? I'm always hesitant of repackaging, as it means a lot of extra try ... catch blocks.
What do you recommend me?
It's impossible to answer this question without an understanding of your code and your coding guidelines, specifically as they relate to exceptions.
But if your coding guidelines permit exceptions, then I suggest a general rule-of-thumb:
Allow exceptions to propagate until they reach a context in which they can be handled properly. If they never reach a context in which they can be handled properly, allow your program to crash. Get a core dump and debug the problem.
"Handling" an exception in a certain context might be as simple as translation it in to an error code or your own exception class, but in this case you should rethrow the new exception and allow it to propagate to a handler.
Don't implement any form of a catch-all handler for exceptions with the intent of preventing your application from crashing, or even to log the error and die. Instead, implement a system which will generate dumps in the event of an unhandled exception, and let your program die. The dump itself is enough of a log. You don't want a catch-all because your system is in such a corrupted state that it can't be recovered from.
I'd recommend your class throws your own exceptions. This way you can enforce information hiding and your class clients won't depend from the libraries you decide to use.
Do you believe it is relevant to the end-user? Because this is something related to design.
Some libraries when used on windows have their own data, while you can use other error handling methods for verifying. For example, sqlite has its own group of error codes, but you can use GetLastError in order to know why the database was not open.
In your case, the question is: Will you use exceptions in your own class? In this case, its better to provide your own exception. If not, you can permit the user to cach your errors and the exception if he wants to.
Claudio.

Is Ogre's use of Exceptions a good way of using them?

I've managed to get through my C++ game programming career so far virtually never touching exceptions but recently I've been working on a project with the Ogre engine and I'm trying to learn properly. I've found a lot of good questions and answers here on the general usage of C++ exceptions but I'd like to get some outside opinions from here on whether Ogre's usage is good and how best to work with them.
To start with, quoting from Ogre's documentation of it's own Exception class:
OGRE never uses return values to indicate errors. Instead, if an error occurs, an exception is thrown, and this is the object that encapsulates the detail of the problem. The application using OGRE should always ensure that the exceptions are caught, so all OGRE engine functions should occur within a try{} catch(Ogre::Exception& e) {} block.
Really? Every single Ogre function could throw an exception and be wrapped in a try/catch block? At present this is handled in our usage of it by a try/catch in main that will show a message box with the exception description before exiting. This can be a bit awkward for debugging though as you don't get a stack trace, just the function that threw the error - more important is the function from our code that called the Ogre function. If it was an assert in Ogre code then it would go straight to the code in the debugger and I'd be able to find out what's going on much easier - I don't know if I'm missing something that would allow me to debug exceptions already?
I'm starting to add a few more try/catch blocks in our code now, generally thinking about whether it matters if the Ogre function throws an exception. If it's something that will stop everything working then let the main try/catch handle it and exit the program. If it's not of great importance then catch it just after the function call and let the program continue. One recent example of this was building up a vector of the vertex/fragment program parameters for materials applied to an entity - if a material didn't have any parameters then it would throw an exception, which I caught and then ignored as it didn't need to add to my list of parameters. Does this seem like a reasonable way of dealing with things? Any specific advice for working with Ogre is much appreciated.
You don't need to wrap every last call to Ogre in try { ... } catch. You do it wherever you can meaningfully deal with the exception. This may be at the individual call site in some cases, or it could be in a high-level loop of some sort. If you can't deal with it meaningfully anywhere, don't catch it at all; let the debugger take over.
In particular, you shouldn't catch exceptions in main() for precisely the reason you cite (at least, not during development; you should in production).
I don't know anything about Ogre, I'm afraid, but the general rule with exception handling is that you catch the exception as far as possible from the throw site, but no further. However, this is only possible if the code that throws the exception uses RAII to look after allocated resources. If the code uses dynamic allocation to plain pointers, or other forms of manual resource management, then you need try-blocks at the call site. If this is the case, I'd say use of exceptions is a bad idea.
You seem to be unaware of how to debug exceptions.
Either
Bring up the VS Debug/Exceptions
dialog and tick the C++ Exceptions
box. This will give you an
opportunity (a dialog appears) to debug when an
exception is thrown.
or
If you've built Ogre source, set a
breakpoint in the Ogre::Exception
constructor and when it attempts to throw one you'll break with
a call stack where the next level up is the throw site.

about c++ exceptions. func() throw()

i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html
it says if i write function() throw(); no exceptions can be thrown in that function. I tried in msvc 2005 writing throw(), throw(int), throw() and nothing at all. each had the exact same results. Nothing. I threw int, char*, another type and it was all caught the same way. It looks like throw doesnt affect it at all. What does function() throw() actually do?
See this article for details on C++ exception specifications and Microsoft's implementation:
Microsoft Visual C++ 7.1 ignores exception specifications unless they are empty. Empty exception specifications are equivalent to __declspec(nothrow), and they can help the compiler to reduce code size.
[...] If it sees an empty exception specification, it will assume you know what you are doing and optimize away the mechanics for dealing with exceptions. If your function throws anyway - well, shame on you. Use this feature only if you are 100% positive your function does not throw and never will.
What you're finding is that that version of VC++ didn't enforce specification exceptions. I believe that was documented as a variance from the standard.
However, exception specifications are usually not a good idea. If a program violates them in a standards-conforming implementation (which the VC++ from VS 2005 was not in this case), the system is supposed to catch it. This means that the specification is not a compiler optimization hint, but rather forces the compiler to go to extra lengths, and sometimes produce suboptimal code.
See the Boost rationale for reasons why the highly regarded Boost project does not use exception specifications. This is Boost, which is something of the poster child for doing weird and useful things with advanced parts of the language.
Quoting from A Pragmatic Look at Exception Specifications:
(Mis)understandings
The second issue has to do with
knowing what you’re getting. As many
notable persons, including the authors
of the Boost exception specification
rationale, have put it,
programmers tend to use exception
specifications as though they behaved
the way the programmer would like,
instead of the way they actually do
behave.
Here’s what many people think that
exception specifications do:
Guarantee that functions will only throw listed exceptions (possibly
none).
Enable compiler optimizations based on the knowledge that only listed
exceptions (possibly none) will be
thrown.
The above expectations are, again,
deceptively close to being correct.
See the link for the full details.
Throwing an exception is not enough, you need a try {} catch() block to catch exceptions. If you don't catch exceptions, std::terminate() is called and your program exits abruptly. Take some time out and have go at this.
throw specifications are designed for two purposes:
To serve as a contract between interface implemented and interface user - you state which exceptions can be throwned from your method, some people consider it part of an interface. (contract) Ala checked exceptions in Java.
As a way to signal the compiler that it can apply certain optimizations in case no exceptions can be thrown from a method/procedure (setting up exception handling costs something)
Throwing an exception not specified in throw() clause is a mistake, however at no point is the implementation required to verify it for you. In fact it's not even possible to verify, as it includes all the possible exceptions from subroutines your subroutine calls. (possibly from other modules) It is not even possible within a single module, as is easily reduced to a halting problem :)