So lets say I have:
struct test {
bool a;
int b;
};
int main()
{
std::unique_ptr<test> ptr;
// don't init the ptr
try
{
if (!ptr->a)
{
std::cout << "ok" << std::endl;
}
}
catch (const std::exception &ex)
{
std::cout << "ex: " << ex.what() << std::endl;
}
return 1;
}
So here I setup a unique pointer, but I don't init it (to simulate this in a larger code base) but I want to catch the exception.
The problem is that my exception is not called - I just get a crash (memory access fault)
I read a few similar questions (but not quite the same) that suggested that I pass the exception by reference - but this did not work.
So is it possible to catch a unique_ptr de-reference exception?
EDIT: I should add that this is on Windows 7 box running MSVS2012 executable - incase it is relevant!
So is it possible to catch a unique_ptr de-reference exception?
There is no unique_ptr dereference exception to catch.
As the documentation says,
The behavior is undefined if get() == nullptr
You can easily write your own smart pointer with this Java-like behaviour, but it does mean paying for a test-and-branch on every dereference, which in general seems silly.
For the slightly different problem described in comments:
I have a list of unique_ptr's - I was trying to avoid individually checking each one by putting a try block around it.
the sane solution is probably to check it once and not on every subsequent dereference:
if(any_of(begin(ptrs), end(ptrs), logical_not<unique_ptr<test>>{})
{
throw MyNullPointerException();
}
Per subsequent comments, you could just add a check-and-throw wrapper to your constructing function.
In C++17 you can almost get what you want by instead returning an optional<unique_ptr<test>> (ie, it either contains a populated unique_ptr, or nothing at all: in that case, calling value to extract the unique_ptr would throw std::bad_optional_access if there isn't really one there).
If you can import ot (or don't have C++17), the GSL is probably even better with gsl::not_null<T>. For example, you could instead store these things in your container
using unique_not_null = std::unique_ptr<gsl::not_null<test>>;
Related
The main problem with throwing exceptions from destructor is that in the moment when destructor is called another exception may be "in flight" (std::uncaught_exception() == true) and so it is not obvious what to do in that case. "Overwriting" the old exception with the new one would be the one of the possible ways to handle this situation. But it was decided that std::terminate (or another std::terminate_handler) must be called in such cases.
C++11 introduced nested exceptions feature via std::nested_exception class. This feature could be used to solve the problem described above. The old (uncaught) exception could be just nested into the new exception (or vice versa?) and then that nested exception could be thrown. But this idea was not used. std::terminate is still called in such situation in C++11 and C++14.
So the questions. Was the idea with nested exceptions considered? Are there any problems with it? Isn't the situation going to be changed in the C++17?
There is one use for std::nested exception, and only one use (as far as I have been able to discover).
Having said that, it's fantastic, I use nested exceptions in all my programs and as a result the time spent hunting obscure bugs is almost zero.
This is because nesting exceptions allow you to easily build a fully-annotated call stack which is generated at the point of the error, without any runtime overhead, no need for copious logging during a re-run (which will change the timing anyway), and without polluting program logic with error handling.
for example:
#include <iostream>
#include <exception>
#include <stdexcept>
#include <sstream>
#include <string>
// this function will re-throw the current exception, nested inside a
// new one. If the std::current_exception is derived from logic_error,
// this function will throw a logic_error. Otherwise it will throw a
// runtime_error
// The message of the exception will be composed of the arguments
// context and the variadic arguments args... which may be empty.
// The current exception will be nested inside the new one
// #pre context and args... must support ostream operator <<
template<class Context, class...Args>
void rethrow(Context&& context, Args&&... args)
{
// build an error message
std::ostringstream ss;
ss << context;
auto sep = " : ";
using expand = int[];
void (expand{ 0, ((ss << sep << args), sep = ", ", 0)... });
// figure out what kind of exception is active
try {
std::rethrow_exception(std::current_exception());
}
catch(const std::invalid_argument& e) {
std::throw_with_nested(std::invalid_argument(ss.str()));
}
catch(const std::logic_error& e) {
std::throw_with_nested(std::logic_error(ss.str()));
}
// etc - default to a runtime_error
catch(...) {
std::throw_with_nested(std::runtime_error(ss.str()));
}
}
// unwrap nested exceptions, printing each nested exception to
// std::cerr
void print_exception (const std::exception& e, std::size_t depth = 0) {
std::cerr << "exception: " << std::string(depth, ' ') << e.what() << '\n';
try {
std::rethrow_if_nested(e);
} catch (const std::exception& nested) {
print_exception(nested, depth + 1);
}
}
void really_inner(std::size_t s)
try // function try block
{
if (s > 6) {
throw std::invalid_argument("too long");
}
}
catch(...) {
rethrow(__func__); // rethrow the current exception nested inside a diagnostic
}
void inner(const std::string& s)
try
{
really_inner(s.size());
}
catch(...) {
rethrow(__func__, s); // rethrow the current exception nested inside a diagnostic
}
void outer(const std::string& s)
try
{
auto cpy = s;
cpy.append(s.begin(), s.end());
inner(cpy);
}
catch(...)
{
rethrow(__func__, s); // rethrow the current exception nested inside a diagnostic
}
int main()
{
try {
// program...
outer("xyz");
outer("abcd");
}
catch(std::exception& e)
{
// ... why did my program fail really?
print_exception(e);
}
return 0;
}
expected output:
exception: outer : abcd
exception: inner : abcdabcd
exception: really_inner
exception: too long
Explanation of the expander line for #Xenial:
void (expand{ 0, ((ss << sep << args), sep = ", ", 0)... });
args is a parameter pack. It represents 0 or more arguments (the zero is important).
What we're looking to do is to get the compiler to expand the argument pack for us while writing useful code around it.
Let's take it from outside in:
void(...) - means evaluate something and throw away the result - but do evaluate it.
expand{ ... };
Remembering that expand is a typedef for int[], this means let's evaluate an integer array.
0, (...)...;
means the first integer is zero - remember that in c++ it's illegal to define a zero-length array. What if args... represents 0 parameters? This 0 ensures that the array has at lease one integer in it.
(ss << sep << args), sep = ", ", 0);
uses the comma operator to evaluate a sequence of expressions in order, taking the result of the last one. The expressions are:
s << sep << args - print the separator followed by the current argument to the stream
sep = ", " - then make the separator point to a comma + space
0 - result in the value 0. This is the value that goes in the array.
(xxx params yyy)... - means do this once for each parameter in the parameter pack params
Therefore:
void (expand{ 0, ((ss << sep << args), sep = ", ", 0)... });
means "for every parameter in params, print it to ss after printing the separator. Then update the separator (so that we have a different separator for the first one). Do all this as part of initialising an imaginary array which we will then throw away.
The problem you cite happens when your destructor is being executed as part of the stack unwinding process (when your object was not created as part of stack unwinding)1, and your destructor needs to emit an exception.
So how does that work? You have two exceptions in play. Exception X is the one that's causing the stack to unwind. Exception Y is the one that the destructor wants to throw. nested_exception can only hold one of them.
So maybe you have exception Y contain a nested_exception (or maybe just an exception_ptr). So... how do you deal with that at the catch site?
If you catch Y, and it happens to have some embedded X, how do you get it? Remember: exception_ptr is type-erased; aside from passing it around, the only thing you can do with it is rethrow it. So should people be doing this:
catch(Y &e)
{
if(e.has_nested())
{
try
{
e.rethrow_nested();
}
catch(X &e2)
{
}
}
}
I don't see a lot of people doing that. Especially since there would be an exceedingly large number of possible X-es.
1: Please do not use std::uncaught_exception() == true to detect this case. It is extremely flawed.
Nested exceptions just add most-likely-ignored information about what happened, which is this:
An exception X has been thrown, the stack is being unwound, i.e. destructors of local objects are being called with that exception “in flight”, and the destructor of one of those objects in turn throws an exception Y.
Ordinarily this means that cleanup failed.
And then this is not a failure that can be remedied by reporting it upwards and letting higher level code decide to e.g. use some alternative means to achieve its goal, because the object that held the information necessary to do the clean up has been destroyed, along with its information, but without doing its cleanup. So it's much like an assertion failing. The process state can be very ungood, breaking the assumptions of the code.
Destructors that throw can in principle be useful, e.g. as the idea Andrei once aired about indicating a failed transaction on exit from a block scope. That is, in normal code execution a local object that hasn't been informed of transaction success can throw from its destructor. This only becomes a problem when it clashes with C++'s rule for exception during stack unwinding, where it requires detection of whether the exception can be thrown, which appears to be impossible. Anyway then the destructor is being used just for its automatic call, not in its cleanup rôle. And so one can conclude that the current C++ rules assume the cleanup rôle for destructors.
The real problem is that throwing from destructors is a logical fallacy. It's like defining operator+() to perform multiplication.
Destructors should not be used as hooks for running arbitrary code. Their purpose is to deterministically release resources. By definition, that must not fail. Anything else breaks the assumptions needed to write generic code.
The problem that may happen during stack unwinding with chaining exceptions from destructors is that the nested exception chain may be too long. For example, you have std::vector of 1 000 000 elements each of which throws an exception in its destructor. Let's assume the destructor of std::vector collects all exceptions from destructors of its elements into single chain of nested exceptions. Then resulting exception may be even bigger than original std::vector container. This may cause performance problems and even throwing std::bad_alloc during stack unwinding (that even couldn't be nested because there is not enough memory for doing that) or throwing std::bad_alloc in other unrelated places in the program.
I've got a few questions about throwing exceptions in C++.
From what I know about them...
An exception can be thrown from within the main() function. Any block of code that can throw an exception in the main() function should be surrounded by try and catch statements as follows
void foo(//args) {
if (...) {
throw "Error reached";
} ...
int main() {
...
try {
//Code that can throw an excpetion
} catch(const char* msg) (
cerr << msg << endl;
}
...
}
In the example above, why is the argument to the catch a const char *. Doesn't C++ allow for strings? Also, is it possible to throw an exception that isn't a const char *, like an int? or a char?
Does throwing an exception in foo, terminate the foo function?
Are there cases where you could put the try and catch statements in the same function as the throw?
Sorry if these are basic questions.
Thanks SO
why is the argument to the catch a const char *
Because you threw string literal which decays to const char*. In short, you catch what you throw.
Doesn't C++ allow for strings?
It does, but to catch string, you need to throw string in first place.
is it possible to throw an exception that isn't a const char *,
You can throw literally anything. It is a good idea to throw special exception classes, like std::exception and derived from it.
Does throwing an exception in foo, terminate the foo function?
Yes, it does.
Are there cases where you could put the try and catch statements in the same function as the throw?
If you want, you can do that. There are not much cases where doing it is a good idea.
It looks like you need to get a good book and read chapter about exceptions. In the meantime this super-FAQ entry might help you/
You can throw an object of any type.
EDIT: (Hopefully I got this right now)
What you have done is throw a C-string, which is of type const char[13] in this case. C-Arrays will decay to pointers to their first element, in this case a pointer of type const char*.
Typically what you want to do is throw a predefined exception object. They can be found in header <stdexcept> and are derived from a base class std::exception. The derived exception classes are for instance std::logic_error, std::range_error, std::bad_alloc etc.
Their constructors take a string as argument, so you can for instance
throw std::logic_error{"Negative values not allowed."};
This message can be accessed in a catch statement like this:
catch(std::exception &e) // capture reference to base class
{
std::cout << e.what() << '\n'; // what() of derived is called, since virtual
}
If an exception is caught, so-called stack unwinding takes place. You can then deal with the error locally, or rethrow the exception. Only when an exception is thrown and never caught, std::terminate() is called an the program aborted.
You can put try/catch statements anywhere. However, remember what the term "exception" actually means. Cases that can easily dealt with using a simple conditional expression if (n < 0) break; or something like that, don't need the exception treatment. Especially if you can realistically expect this kind of unwanted condition to be true often. Then it is not something "exceptional".
If you decide to deal with an error using exceptions and they can not be treated locally, you may put try/catch clauses around the beginning and end of main().
Since you can put several catch statements directly after a try statement, you can then begin to deal with more specific errors, or simply catch anything via catch(...) { //... }.
This is all described in great detail (including pointers on when and when not to use it, in the C++ FAQ.
EDIT: Here's an example that makes use of try/catch statements. However, not an exception object is caught, but an int (errno). Just to show, that you can really throw/catch anything you like. Let process_several_files() be a function somewhere nested in your code:
std::vector<std::string> process_several_files(std::vector<std::string> const& files)
{
std::vector<std::string> contents{};
contents.reserve(files.size()); // files contains file names from user input
for (auto const& file : files)
{
try
{
contents.emplace_back(get_file_contents(file.c_str())); // A "C like" function. get_file_contents() will throw "errno", if a file does not exist
}
catch(int err)
{
std::cerr << "***Error while opening " << file << " : " << std::strerror(err) << "***\n";
continue; // "scope" didn't change, just keep iterating!
}
}
return contents;
}
I know this may look as a duplicate, but the other questions does not have the answer that I am looking for.
template <class T>
T Queue<T>::pop_back()
{
try
{
if (empty())
throw "The Queue is empty!";
size--;
back= (back- 1 + max_size) % (max_size);
return queue[back];
}
catch (char* strException)
{
cerr << "Error: " << strException << endl;
}
}
I have the above implementations of functions pop_back. Every time it removes the element it has to return that element. However, every time it tries to pop something from an empty Queue it says " terminate called after throwing an instance of 'char const*'
Aborted (core dumped)"
What are some tips and advice to fix this issue? I have a try catch block there but it is not helping much. The functions has to return something, or stop and return to main, from where it was called.
I am looking for a design patter so I can used it other times I encounter such a problem.
Basically what should I do when I need to return something but there is nothing I can return?
Thank you.
You should not catch the exception in the function itself - the exceptions should be used to communicate the problem to the callers - they're the ones who should know what's sensible handling in this situation.
The functions has to return something, or stop and return to main, from where it was called.
Not exactly - as above, it can throw and be continued from the catch() block most closely enclosing the place from which it was called.
Keep in mind that caller's who'd rather not do exception handling could easily test empty() or size() before deciding to attempt the pop... so it's quite reasonable to throw the exception back to them.
Further, don't throw a string literal... use a std::runtime_error("...") instead. Reasons here.
I am looking for a design patter so I can used it other times I encounter such a problem. Basically what should I do when I need to return something but there is nothing I can return?
As mentioned, throwing an exception is a normal way to deal with this, especially for something like pop from an empty stack. More generally though, other alternatives include:
return a sentinel value
for example, if the container stores ints, you might be able to return 0 or -1 or numeric_limits<int>::max, or for std::strings perhaps an empty one, but what would be best depends on the usage to which the client code puts the container
for a templated container it's hard to know whether a value of T can be used for this - for example if T is char and content from a binary file is being stored in the container, any character value you might want to use as a sentinel might also appear in the file; if you want to try this anyway, it's usually best to ask the caller to provide a sentinel value as a template parameter or constructor parameter.
return a std::optional<T> (still pending standardisation but might be offered by some compilers) or boost::optional<T>
return a std::pair<bool, T> where .second is only the popped value if .first is true, indicating success
return a [smart] pointer, but that's clumsy as you may have to dynamically allocate memory for a copy of the popped value; a smart pointer helps ensure the caller deletes the object later
just document that the caller must check empty() or size() first, and asserting that the container's not empty from within pop_back - this would halt the program if this "precondition" is not met by the caller
change the interface to bool pop_back(T& t) where the return value indicates success and that caller-specified T parameter will have been set.
None of these are worthy of being called "patterns" - patterns tend to refer to more complicated, high-level design aspects.
Replace the current code
template <class T>
T Queue<T>::pop_back()
{
try
{
if (empty())
throw "The Queue is empty!";
size--;
back= (back- 1 + max_size_) % (max_size);
return queue[back];
}
catch (char* strException)
{
cerr << "Error: " << strException << endl;
}
}
with something like this:
template< class Type >
auto Queue<Type>::pop_back()
-> Type
{
if( empty() ) { throw std::runtime_error( "The Queue is empty!" ); }
--size;
back = (back - 1 + max_size_) % (max_size_); // Note fix of name
return queue[back];
}
You want to throw the exception to the caller.
Using an exception within the function, to just jump to the code that outputs some text (and fails to return a function result), doesn't make sense.
In passing, it's a good idea to standardize on a single naming convention for members. The inadvertent mixing of max_size_ and max_size, which was most probably a bug, and anyway a serious imperfection, would not have happened with a single naming convention. And as it is, back as a data member is inconsistent with max_size_.
Also note the use of std::runtime_error instead of a string literal as the exception object. This supports catching of a std::exception. You might find some other standard exception class more suitable, more indicative of the particular problem, but I generally just use std::runtime_error.
Another reason why it's a good idea to use the standard exception classes is that it's otherwise easy to specify the wrong type in a catch.
For example, the catch in your code would not have worked:
#include <iostream>
using namespace std;
void foo()
{
try
{
throw "Blah blah!";
}
catch( char* s )
{
cout << "Caught exception internally in foo(): " << s << endl;
return;
}
}
auto main() -> int
{
try
{
foo();
}
catch( ... )
{
cout << "Failed to catch exception in foo()." << endl;
}
}
Output:
Failed to catch exception in foo().
Precissely the issue is that you have try-catch block.
If an exception is thrown, execution flow will go to the catch logic. It will print your statement, and the function will end without returning a value.
Re-throw the exception of add a return statement, either at the end of the catch block or at the end of the function (rethrowing seems the best option, since you do not handle the exception at all).
Whenever I output a particular pointer address to std::cout, I get a crash:
bool MyClass::foo() const
{
std::cout << "this prints fine" << std::endl << std::flush;
std::cout << d << std::endl << std::flush; // crash!
return true;
}
Where d is a pointer member of the class, i.e.:
class MyClass {
// ...
private:
MyClassPrivate* d;
};
What could cause the application to crash? Even if it is a NULL pointer, or an initialized pointer, it should still print out the (perhaps invalid) address, right?
The application is compiled in debug mode, if that makes a difference. The function foo is not marked as inline.
Background: I am trying to track down a bug in an external application process. The bug is only caused when another application sends rapid-fire command to the process. I'm using std::cout to trace the execution of the external process.
If this is not a valid pointer, any access to a member field might cause an access violation. Non-virtual methods called on invalid pointers work just fine until they try to access a field, because the call itself doesn't need to dereference this.
For instance, this situation would crash roughly as you describe:
MyClass* instance = nullptr; // or NULL if you're not using C++11
instance->foo(); // will crash when `foo` tries to access `this->d`
There could be an overload of operator<<(ostream &, MyClassPrivate*), that dereferences the pointer. For example there certainly is if MyClassPrivate is really char.
Try std::cout << (void*)d;, see whether or not it makes a difference. If not, zneak's answer seems plausible.
When I use boost::copy_exception to copy an exception to an exception_ptr, I lose type information. Take a look at the following code:
try {
throw std::runtime_error("something");
} catch (exception& e) {
ptr = boost::copy_exception(e);
}
if (ptr) {
try {
boost::rethrow_exception(ptr);
} catch (std::exception& e) {
cout << e.what() << endl;
cout << boost::diagnostic_information(e) << endl;
}
}
From this, I get the following output:
N5boost16exception_detail10clone_implISt9exceptionEE
Dynamic exception type: boost::exception_detail::clone_impl<std::exception>
std::exception::what: N5boost16exception_detail10clone_implISt9exceptionEE
So basically boost::copy_exception statically copied the argument it got.
This problem is solved if I throw my exception with boost::enable_current_exception instead, like this.
try {
throw boost::enable_current_exception(std::runtime_error("something"));
} catch (...) {
ptr = boost::current_exception();
}
if (ptr) {
try {
boost::rethrow_exception(ptr);
} catch (std::exception& e) {
cout << e.what() << endl;
cout << boost::diagnostic_information(e) << endl;
}
}
The problem with this is that sometimes the exceptions are thrown by a library that does not use boost::enable_current_exception. In this case, is there any way to put the exception into an exception_ptr apart from catching each kind of possible exception one by one and use boost::copy_exception on each one?
This is by design, and your analysis is correct: the static type is used, not the dynamic type. In fact, to avoid this surprise, boost::copy_exception became std::make_exception_ptr during the process that led to C++11. That way, it's clearer that current_exception (whether the Boost version or the C++11 one) is the correct thing to use to correctly capture, well, the current exception. I highly recommend using BOOST_THROW_EXCEPTION, or at least boost::throw_exception, when it comes to using Boost.Exception-enabled exceptions in your code.
When it comes to third-party code, there is no solution other than the one you already mentioned, or some other moral equivalent (like dynamic_cast-ing to the different leaf classes that make up the exception type hierarchy or hierarchies, or typeid abuse).
In this respect exception handling is the same as working with one or more hierarchy of polymorphic types, and the operation you're attempting in this case is a virtual copy, also known as cloning: either your code is intrusively designed to support that (as is the case when using e.g. BOOST_THROW_EXCEPTION(e); instead of throw e;), or you're going to painfully walk the inheritance tree. Note that you can at least refactor that pain into just one site, such that you'd end up with e.g.
try {
third_party_api();
} catch(...) {
ptr = catch_exceptions_from_api();
}