Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
int main(int argc, char** argv) {
  Object object;
  bool error = false;

  try {
    // do useful work
  } catch (...) {
    error = true;
  }

  return error ? -1 : 0; // object gets destroyed here
}

Compliant Solution (main())

An alternative is to wrap all of main()'s functionality inside a try-catch block.

Code Block
bgColor#ccccff

int main(int argc, char** argv) {
  try {
    Object object;
    // do useful work
    return 0; // object gets destroyed here
  } catch (...) {
    throw;  
  }
}

Risk Assessment

Failing to handle exceptions can lead to resources not being freed, closed, etc.

...