Questions tagged [exception-safety]

93 questions
4
votes
4 answers

Exception specification, is it useful or not?

first disclaimer: This is not to cause "language wars". I really need this (the clarification on the subject) for my report, and I just want to have valid and solid arguments. Ok, so here is the question: In C++ exception specification has been…
smallB
  • 16,662
  • 33
  • 107
  • 151
4
votes
3 answers

Should I clear() containers passed in as a parameter, or swap in a new object?

I'm in a discussion at work as to how to properly handle containers as parameters. We have a function that takes in a container parameter, and wants to return the container filled ONLY with what the function puts into it: class bar; void…
Taeolas
  • 265
  • 2
  • 7
3
votes
6 answers

How do you know all the exceptions a method can throw

Is there a way to get some details regarding exception safety aspects of Java's standard classes? Mainly working with C++ and C#, I'm confused with Java exception specifications, so I need to understand the proper way of working with exceptions. To…
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
3
votes
1 answer

On Exception-Safety in a Function-Call

Is the call to f() exception-safe? inline std::auto_ptr auto_new() { return std::auto_ptr(new C()); } void f(std::auto_ptr p1, std::auto_ptr p2); // ... { f(auto_new(), auto_new()); } In other words, does it make any…
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
3
votes
2 answers

Does nothrow/noexcept are sufficient to say we have no-throw guarantee?

In Exception Safety as created by Abrahams we have the three guarantees: basic, strong and no-throw guarantee. Can i say that if i have a codebase using nothrow for "new" and noexcept for method signatures that i have the no-throw guarantee?…
3
votes
3 answers

Is there a way not to kill an Qt application which throwed a std::bad_alloc?

Exception safety is really important in Modern C++. There is already a great question about exception safety here. So I am not talking about Exception safety in general. I am really talking about exception safety with Qt in C++. There is also a…
Arnaud
  • 3,765
  • 3
  • 39
  • 69
3
votes
1 answer

C++ unordered_map exception safety

I was wandering the C++ specifications (cplusplus.com) and found that there's nothing told about exception safety for std::unordered_map so basically if I write map["foo"]=5; and an exception is thrown because I'm out of memory or bad_alloc, what…
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
3
votes
2 answers

What prevents the compiler's optimization to reorder strongly exception-safe code?

Given Jon Kalb's strongly exception-safe code to solve the Cargill Widget example, what prevents the compiler from re-organizing the operations and thus making the code not strongly exception-safe? #include // std::swap template<…
Charles L Wilcox
  • 1,126
  • 8
  • 18
2
votes
4 answers

C++ exception safety paranoia: how much is too much?

The strong exception safety guarantee says that an operation won't change any program state if an exception occurs. An elegant way of implementing exception-safe copy-assignment is the copy-and-swap idiom. My questions are: Would it be overkill to…
Mukul M.
  • 540
  • 1
  • 5
  • 15
2
votes
1 answer

Throwing exception in constructor before member initializer list?

As an example, say I have the class class A{ B& foo; }; and I want to initialize this class with a constructor that takes in a vector and an index (just for example). So I get explicit A(std::vector& lst, int index): …
k huang
  • 409
  • 3
  • 10
2
votes
1 answer

Is std::map exception safe with custom comparator?

What std::map will do if custom comparator throw an exception during rebalancing? Apparently, it should remember all the previous turns and return everything to its original state. Is it true?
dasfex
  • 1,148
  • 2
  • 12
  • 30
2
votes
1 answer

Exception safety of new operator

This is implementation of new operator in libstdc++: _GLIBCXX_WEAK_DEFINITION void * operator new (std::size_t sz) _GLIBCXX_THROW (std::bad_alloc) { void *p; /* malloc (0) is unpredictable; avoid it. */ if (__builtin_expect (sz == 0,…
2
votes
2 answers

Are allocator construct and destroy member functions allowed to throw exceptions from internal logic?

I'm writing my own container that requires nothrow movable and copyable types. I thought I could simplify logic a bit when it comes to exception safety. But I noticed that construct and destroy member functions of allocators have no wording about…
user3624760
2
votes
1 answer

Why std::unique_ptr does not explicitly require a noexcept Deleter?

The documentation says that Deleter should be: nothrow constructible nothrow callable (because it's called from ~unique_ptr() noexcept nothrow destructible (for the reason above) My question is why uniqut_ptr is defined to allow a Deleter that may…
Kentzo
  • 3,881
  • 29
  • 54