Questions tagged [exception-safe]
17 questions
10
votes
2 answers
Can a stack have an exception safe method for returning and removing the top element with move semantics?
In an answer to a question about std::stack::pop() I claimed that the reason pop does not return the value is for exception safety reason (what happens if the copy constructor throws).
@Konrad commented that now with move semantics this is no…

Motti
- 110,860
- 49
- 189
- 262
9
votes
5 answers
Exception Safety- When, How, Why?
I'm just a fledgling programmer that at least tries to program more than the best-case scenario. I've been reading Herb Sutter's "Exceptional C++" and went through the exception-safety chapters thrice so far. However, barring the example he posed (a…

IAE
- 2,213
- 13
- 37
- 71
6
votes
4 answers
Is it safe to push_back 'dynamically allocated object' to vector?
Whenever I need to add dynamically allocated object into a vector I've been doing that the following way:
class Foo { ... };
vector v;
v.push_back(new Foo);
// do stuff with Foo in v
// delete all Foo in v
It just worked and many others…

upriser
- 192
- 1
- 7
4
votes
3 answers
Is it OK to have a throwing swap member-implementation?
The general guideline when writing classes (using the copy-and-swap idiom) is to provide a non throwing swap member function. (Effective C++, 3rd edition, Item 25 and other resources)
However, what if I cannot provide the nothrow guarantee because…

Martin Ba
- 37,187
- 33
- 183
- 337
4
votes
4 answers
Is this a fine std::auto_ptr<> use case?
Please suppose I have a function that accepts a pointer as a parameter. This function can throw an exception, as it uses std::vector<>::push_back() to manage the lifecycle of this pointer. If I declare it like this:
void manage(T *ptr);
and call it…

Gui Prá
- 5,559
- 4
- 34
- 59
4
votes
1 answer
Scope Guard Statement in C#
The Resource Acquisition Is Initialization (RAII) idiom and the try-finally statement form the backbone of the traditional approaches to writing exception safe programming.
My question is: Is there something like Scope Guard Statement available on…

Glauco Vinicius
- 2,527
- 3
- 24
- 37
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
2
votes
2 answers
safe std::tr1::shared_ptr usage
Is this approach unsafe?
#include
Foo * createFoo()
{
return new Foo(5);
}
int main()
{
std::tr1::shared_ptr bar(create());
return 0;
}
Or would it be preferable for createFoo to return a shared_ptr object?

tmatth
- 499
- 4
- 14
2
votes
3 answers
How do I describe a method that has no side effects if an exception is thrown during execution?
I just can't remember the terminology used for this and other related properties.
EDIT - Maybe such a concept doesn't exist but I remember reading something in Effective C++ (or More Effective C++) where he advocated using swaps to commit changes…

tpower
- 56,100
- 19
- 68
- 100
1
vote
6 answers
Is synchronized keyword exception-safe?
Possible Duplicate:
Side effects of throwing an exception inside a synchronized clause?
I am wondering if synchronized is exception-safe? Say, an uncaught exception happens within the synchronized block, will the lock be released?

user705414
- 20,472
- 39
- 112
- 155
1
vote
7 answers
Is Try/Finally actually exception-safe?
Let's say you have a piece of code like:
resource = allocateResource();
try { /* dangerous code here */ }
finally { free(resource); }
I'm not referring to any specific language here, but I guess Java, C#, and C++ would be good examples (assuming…

user541686
- 205,094
- 128
- 528
- 886
1
vote
1 answer
Can a unique_ptr<>() initialization fail?
From the documentation of std::unique_ptr<>(), what may happen when initializing the pointer is not clear to me.
When allocating an std::shared_ptr<>(), it allocates a memory buffer to handle the reference counter. So I may get an std::bad_alloc…

Alexis Wilke
- 19,179
- 10
- 84
- 156
1
vote
2 answers
Why does `myvector.push_back(autoPtr.release())` provide the strong exception safety guarantee?
EDIT: I should've mentioned, I was looking at the documentation for Boost's ptr_sequence_adapter and it claims that their adapter for template< class U > void push_back( ::std::auto_ptr x ); is equivalent to doing…

Billy ONeal
- 104,103
- 58
- 317
- 552
1
vote
3 answers
Do C standard library functions which are included in C++ throw exception?
In the below code, author points that new operator function call might cause an exception so that this implementation is not exception safe because object state is already changed in the first line.
String &String::operator =( const char *str ) {
…

Validus Oculus
- 2,756
- 1
- 25
- 34
0
votes
1 answer
Exception-safe logging in java
I have a (probably) weird question about logging in an exception-safe way in java.
Let's say I have this code in java:
if (log.isDebugEnabled())
{
log.debug(...expression...);
}
Is there a best practice for logging ...expression.. in a way that…

Rossomoto
- 9
- 1