Linked Questions

2 votes
2 answers
1k views

Why cannot throw exceptions from destructors but can from copy constructor? [duplicate]

It seems you cannot throw exceptions from destructors in case more than one exception is thrown from the destructor. In Effective C++ Scott Meyers uses a vector example where the first element ...
user997112's user avatar
  • 29.9k
1 vote
3 answers
300 views

c++: Throwing exception from a destructor [duplicate]

This is an interview question. I told him that program may terminate if stack unwinding is already in progress.Other than that do you see any problem given that the exception is properly handled. I ...
rahul's user avatar
  • 6,457
-1 votes
2 answers
277 views

exception thrown in DTOR [duplicate]

I'm new to C++, and somewhat confused about the following matter: What happens when a DTOR throws an exception? Is the object still being destroyed? Thanks in advance.
cookya's user avatar
  • 3,189
-2 votes
1 answer
403 views

What happens when i throw an exception in c++ destructor? [duplicate]

Possible Duplicate: throwing exceptions out of a destructor In C++ we should never throw an exception in the destructor . Does this code works as intended ? struct a { ~a( ) { } }; struct ...
brett's user avatar
  • 5,479
0 votes
1 answer
161 views

when function exits with 2 exceptions, what happens to the original exception [duplicate]

NOTE: I am using gcc 4.4.7 on redhat linux 6.3. The question in below example is about what GCC does to first exception thrown from A::doSomething() and NOT about whether exceptions should be thrown ...
Jimm's user avatar
  • 8,335
1 vote
0 answers
161 views

Why does the program terminates abruptly? [duplicate]

#include <iostream> using namespace std; class B { public: ~B() { throw 42; } }; int main() { try { B obj; throw 32; } catch( int i ) { cout<<i<...
Dinesh Gowda's user avatar
  • 1,104
394 votes
11 answers
182k views

Where do I find the current C or C++ standard documents?

For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online. Googling can sometimes feel futile, again especially for the C standards, since ...
340 votes
16 answers
315k views

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

Does C++ support 'finally' blocks? What is the RAII idiom? What is the difference between C++'s RAII idiom and C#'s 'using' statement?
Kevin's user avatar
  • 25.7k
260 votes
4 answers
107k views

Do I need to manually close an ifstream?

Do I need to manually call close() when I use a std::ifstream? For example, in the code: std::string readContentsOfFile(std::string fileName) { std::ifstream file(fileName.c_str()); if (file....
Gustavo Muenz's user avatar
55 votes
6 answers
18k views

When is a function try block useful?

I'm wondering when programmers use function try blocks. When is it useful? void f(int i) try { if ( i < 0 ) throw "less than zero"; std::cout << "greater than zero" << ...
Sarfaraz Nawaz's user avatar
53 votes
8 answers
10k views

RAII vs. exceptions

The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail, in which case ...
16 votes
15 answers
2k views

What do I need to know about memory in C++?

I've been doing my best to learn C++ but my previous training will fall short in one major issue: memory management. My primary languages all have automatic garbage collection, so keeping track of ...
cwallenpoole's user avatar
37 votes
2 answers
13k views

Why is my exception-in-destructor not triggering std::terminate?

I am well aware of the fact that one should not throw any exception in destructor. But as a part of making my grip on this concept,I coded this example :- #include <iostream> class A { private: ...
amit singh's user avatar
20 votes
3 answers
20k views

using custom deleter with unique_ptr

With shared_ptr you can use a custom deleter, like: auto fp = shared_ptr<FILE>( fopen("file.txt", "rt"), &fclose ); fprintf( fp.get(), "hello\n" ); and this will remember to fclose the ...
sp2danny's user avatar
  • 7,606
13 votes
2 answers
10k views

c++ exception in destructor

From other threads, I know we should not throw exception in destructor! But for below example, it really works. Does that means we can only throw exception in one instance's destructor? how should we ...
user avatar

15 30 50 per page
1
2 3 4 5 6