I have some technical questions. In this function:
string report() const {
if(list.begin() == list.end()){
throw "not good";
}
//do something
}
If I throw the exception what is going on with the program? Will my function terminate or will it run further? If it terminates, what value will it return?
If you throw an exception, all functions will be exited back to the point where it finds a try...catch block with a matching catch type. If your function isn't called from within a try block, the program will exit with an unhandled exception.
Check out https://isocpp.org/wiki/faq/exceptions for more info.
It will basically go up the stack until it finds an exception handler; if it gets to the end of the stack without finding one, your program will crash. If it does find one, it will rewind the stack up that point, run the handler, and continue with the code after the handler block, however far up your stack that may be.
You can get all sorts of details about C++'s exception handling mechanism through Google. Here's a head start.
Since you're not catching the exception within the context of the function, the function will terminate and the stack will be unwound as it looks for an exception handler (a catch block that would match either string, or the generic catch(...)). If it doesn't find one, your program will terminate.
Your function will terminate immediately, and it won't return anything. If there are no catch statements catching the exception "up the call chain", your application will terminate.
It won't return, it will in fact terminate and reach the "nearest" (call-stack-wise) try...catch block.
If none is found, most of the time the program just exits, on some platforms the error can be printed, I don't know the specifics of that though (and most likely only the ones derived from std::exception).
Related
I have a non-void function which returns a value to be used as an input of a class constructor, but there is a possibility for that value not to be available, so I want to return the value in case of success or just terminate (std::terminate()) the application in case of failure before the function return.
Since std::terminate is called by the runtime according to https://en.cppreference.com/w/cpp/error/terminate, am I obliged to throw an exception and let the runtime decide whether the program is terminated or not?
What about the following warning I read in a simple test I just performed? "terminate called without an active exception"
Am I going in a stupid way?
Thanks for your insights in advance!
... I obliged to throw an exception and let the runtime decide whether the program is terminated or not?
It is up to you. If you want to be able for an exception to be caught somewhere up the call stack then you should throw an exception. In cases you want to terminate because there is no way to recover then calling std::terminate is fine. Just consider that if you throw an exception which is not caught then the program terminates anyhow, so you can choose to either catch it or not, while std::terminate terminates always.
The same page that you linked to, in your question, also has the following statement:
std::terminate() may also be called directly from the program.
I need clarification on how a try block works with recursion.
Say I have a function called thisFunction with this structure:
thisfunction()
{
if (...)
{
//...
}
else
{
if()
{
try {
thisFunction()
}
catch(...) {
throw exception()
}
//...
}
}
}
}
When this runs, if the program ever goes into the try, what happens?
Does thisFunction() run once before going to the catch if it happens to re-run and fall into the try block again?
Will it ever even go to the catch(...)?
Recursion is still a function call like any other, it just happens to be a function calling itself.
Each call to thisFunction() has its own try/catch block when calling the next iteration. If any iteration of thisFunction() throws an exception, the exception will be caught by the nearest matching catch block, as expected.
If that happens to be the catch of a previous thisFunction() iteration, then that iteration's catch will throw a new exception that will be handled like any other exception. For multiple iterations of thisFunction, the exceptions will continue being caught and thrown until eventually an exception escapes the initial thisFunction() call, and will either be caught or not depending on the code that called thisFunction() for the first time to start the recursion.
When this runs if the program ever goes into the try, what happens?
The same thing that happens when the try/catch scope is entered, at any other time.
Does thisFunction() run once
Since thisFunction() is invoked immediately, as a first order of business inside the try block, then thisFunction() will be invoked when the try block is entered. No surprises here.
Will it ever even go to the catch(...)
If an exception is thrown inside the try block, then, of course, the catch block will catch it, provided that the thrown exception matches the catch.
The recursive call does not change that. If an exception gets thrown in the recursive call to this function, then the exception will or will not be caught in exactly the same way as if the recursive call was not made, and the same exception was thrown anywhere else inside the same try block.
Once the execution enters the try block, the die is cast. If a matching exception gets thrown, it will be caught by the catch block. This remains true until the execution thread naturally leaves the try scope. Until then, if a matching exception is thrown it will be caught.
Now, if the recursive call entered the same try block, and an exception gets thrown, the exception is going to get caught by the catch block from the recursive call. When an exception gets thrown, it gets caught by the nearest matching catch block, in the execution thread, and the stack gets unwound to that point.
This one just similar to nested try catch block, every try has it respective catch in its class stack and for respective try it will execute respective catch block if exception occurs.
I am running into an issue here because the return of stack.front() isn't pointing to anything when the stack is empty. So despite catching the exception, I end up getting bad access run-time errors.
Is there a better way to go about doing this? I want the program to continue functioning, even when certain functions are called and have errors.
You're running into issues because you immediately catch exception after throwing it. The exception doesn't get out of top() and you're calling getFront() on an empty stack. Rethrow your exception in the catch block or don't catch it all.
I have some C++ code that looks like this:
void Student::addCourse(Course cVal, string gr) throw(...) {
try {
GradedCourse c(cVal, gr); // If an exception is thrown here...
coursesTaken.insert(c); // will this statement be executed?
} catch(...) {
throw;
}
}
The GradedCourse constructor may throw an exception if gr, which contains a grade for a course, is found to be invalid by the constructor. If such an exception occurs, will any further statements inside the try block be executed? Can I be sure that such an exception will result in no attempt to insert the GradedCourse into coursesTaken (which is an STL set)? I've searched both Stack Overflow and Google without much success.
No.
If GradedCourse c(cVal, gr); throws an exception, nothing else inside the try block will be executed.
Now I understand what you are trying to ask, but your title and question itself are both asking conflicting things. :)
If an exception is thrown inside of a try block, execution immediately jumps to the catch block that handles that exception, bypassing all other statements.
Here is the documentation on exceptions. It does not directly address your issue, but it does cover other important things such as exception nesting, or chaining exception handlers.
What's the difference between those three, and how shall I end program in case of exception which I can't handle properly?
abort indicates "abnormal" end to the program, and raises the the POSIX signal SIGABRT, which means that any handler that you have registered for that signal will be invoked, although the program will still terminate afterwords in either case. Usually you would use abort in a C program to exit from an unexpected error case where the error is likely to be a bug in the program, rather than something like bad input or a network failure. For example, you might abort if a data structure was found to have a NULL pointer in it when that should logically never happen.
exit indicates a "normal" end to the program, although this may still indicate a failure (but not a bug). In other words, you might exit with an error code if the user gave input that could not be parsed, or a file could not be read. An exit code of 0 indicates success. exit also optionally calls handlers before it ends the program. These are registered with the atexit and on_exit functions.
std::terminate is what is automatically called in a C++ program when there is an unhandled exception. This is essentially the C++ equivalent to abort, assuming that you are reporting all your exceptional errors by means of throwing exceptions. This calls a handler that is set by the std::set_terminate function, which by default simply calls abort.
In C++, you usually want to avoid calling abort or exit on error, since you're better off throwing an exception and letting code further up the call stack decide whether or not ending the program is appropriate. Whether or not you use exit for success is a matter of circumstance - whether or not it makes sense to end the program somewhere other than the return statement in main.
std::terminate should be considered a last-ditch error reporting tool, even in C++. The problem with std::terminate is that the terminate handler does not have access to the exception that went unhandled, so there's no way to tell what it was. You're usually much better off wrapping the entirety of main in a try { } catch (std::exception& ex) { } block. At least then you can report more information about exceptions that derived from std::exception (although of course exceptions that do not derive from std::exception would still end up unhandled).
Wrapping the body of main in try { } catch(...) { } isn't much better than setting a terminate handler, because again you have no access to the exception in question. There is at least one benefit, though: whether stack unwinding is done when an exception goes completely uncaught is implementation defined, so if you need guaranteed stack unwinding, this would be a way to get that.
std::abort and std::exit (and more: std::_Exit, std::quick_exit) are just lower level functions. You use them to tell the program what you want it to do exactly: what destructors (and if) to call, what other clean-up functions to call, what value to return, etc.
std::terminate is a higher level abstraction: it is called (by either run-time or you) to indicate that an error in the program occurred and that for some reason it is not possible to handle by throwing an exception. The necessity for that typically occurs when error occurs in the exception mechanism itself, but you can use it any time when you do not want your program to continue beyond the given error. I compiled the full list of situations when std::terminate is called in my post. It is not specified what std::terminate does, because you are in control of it. You can configure the behavior by registering any functions. The limitations you have are that the function cannot return back to the error site and it cannot exit via an exception, but technically you can even start your message pump inside. For the list of useful things that you can do inside, see my other post.
In particular, note that std::terminate is considered an exception handler in contexts where std::terminate is called due to a thrown exception that could not be handled, and you can check what the exception was and inspect it by using C++11 using std::rethrow_exception and std::current_exception. It is all in my post.
quick_exit() !
If your program is multi-threaded, then calling exit() will most likely result in a crash because global/static std::thread objects will be attempted to destruct without exiting their threads.
If you want to return an error code and exit the program (more or less) normally, call quick_exit() in multi-threaded programs.
For abnormal termination (without a possibility for you to specify the error code), abort() or std::terminate() can be called.
Note: quick_exit() has not been supported by MSVC++ until version 2015 .
terminate() is automatically called
when an exception occurs that cannot
be handled. By default, terminate()
calls abort(). You can set a custom
handle with set_terminate() function.
abort() sends the SIGABRT signal.
exit() is not necessarily a bad
thing. It successfully exits the
application, and calls atexit()
functions in LIFO order. I don't
normally see this in C++
applications, however, I do see it in
many unix based applications where it
sends an exit code at the end.
Usually a exit(0) indicates a
successful run of the application.
terminate leaves you the possibility to register what will happen when it is called. Should be one of the other two.
exit is a normal exit allowing to specify an exit status. Handlers registered by at_exit() are run
abort is an abnormal exit. The only thing which is ran is the signal handler for SIGABRT.
My advice would be not to use any of them. Instead, catch the exceptions you can't handle in main() and simply return from there. This means that you are guaranteed that stack unwinding happens correctly and all destructors are called. In other words:
int main() {
try {
// your stuff
}
catch( ... ) {
return 1; // or whatever
}
}