Bloomberg LP Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

The only way to catch an error in constructors is by using exceptions.
Exception from destructor is illegal. If destruction is happening in an exception, and if this destructor throws an exception, result becomes undefined.

- Kiran April 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is good practice to throw and catch exception in destructors. You better catch or else they propagate and may terminate a running program.
Check Meyers's More Effective C++ for explanation.
Exception are common in constructors as well but be careful when to use them since they are expensive.

- souaaz May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You cannot throw an exception from a destructor. It's not allowed in C++. However, you can throw an exception from a constructor since it does not have a return type.

- Anonymous May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

By not allowed, I meant that the compiler will not be able to decide what to do with the exception.

- Anonymous May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's allowed. Just try it.
The problem will be when another (or the same but for another object) destructor throws an exception during stack unwinding.
In terms of exception handling, destructors are special because they are the only functions get called during stack unwinding. NOTE: the compiler will not be able to decide what to do with TWO exceptions at the same time. ONE exception is perfectly ok.

Well... constructors of exception objects also can be invoked. The result will be the same: two exceptions => terminate().

- Anonymous June 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I second that Meyer's MEC++ has good explanations.
FWIW, the "Handling an exception" subsection(15.3) from the latest C++ draft N3090 from open-std.org also gives more details if you're adventurous (it's free)

- Anonymous May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came across this page which had a detailed explanation.

"If during stack unwinding a destructor throws an exception and that exception is not handled, the terminate() function is called."

Code taken from the public library of IBM.

#include <iostream>
using namespace std;

struct E {
  const char* message;
  E(const char* arg) : message(arg) { }
};

void my_terminate() {
  cout << "Call to my_terminate" << endl;
};

struct A {
  A() { cout << "In constructor of A" << endl; }
  ~A() {
    cout << "In destructor of A" << endl;
    throw E("Exception thrown in ~A()");
  }
};

struct B {
  B() { cout << "In constructor of B" << endl; }
  ~B() { cout << "In destructor of B" << endl; }
};

int main() {
  set_terminate(my_terminate);

  try {
    cout << "In try block" << endl;
    A a;
    B b;
    throw("Exception thrown in try block of main()");
  }
  catch (const char* e) {
    cout << "Exception: " << e << endl;
  }
  catch (...) {
    cout << "Some exception caught in main()" << endl;
  }

  cout << "Resume execution of main()" << endl;
}

Output :

In try block
In constructor of A
In constructor of B
In destructor of B
In destructor of A
Call to my_terminate

Throw is called in the destructor of A which isn't handled, so the program calls terminate.

- AgnelCodes July 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A C++ destructor throws or may throw an exception.

C++ destructors should never throw exceptions for two reasons. First, C++ objects can be torn down implicitly in contexts, where exceptions cannot be allowed. For example, suppose you enter a block and declare an instance of an object X whose destructor will throw an exception E1. Now suppose you exit that block by throwing another exception, E2. The C++ runtime cannot execute the exception handlers for both E1 and E2, and choosing one would effectively lose the other exception. Since losing exceptions is unacceptable, the C++ runtime calls terminate() which normally kills the process immediately.

Second, even if an exception thrown by a destructor is successfully thrown and caught, it is still bad because it causes the application to leak memory. When an object is deleted, two things happen: first the destructor is called and second the delete operator is called. It is the delete operator that actually releases the storage. If the destructor throws an exception the delete operator is never called so you will leak memory.

software.intel.com/sites/products/documentation/doclib/iss/2013/sa-ptr/sa-ptr_win_lin/GUID-D2983B74-74E9-4868-90E0-D65A80F8F69F.htm

- Anonymous November 27, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More