C++

Topic: Exceptions

What does it mean that exceptions separate the "good path" (or "happy path") from the "bad path"? 

It's another benefit of exceptions over return-codes.The "good path" (sometimes called the "happy path") is the control-flow path that happens when everything goes well — when there are no problems.The "bad path" (or "error path") is the path that control-flow takes when something goes wrong — when there is a problem.Exceptions, when done right, separate the happy path from the error path.Here is a simple example: function f() is suppoesd to call functions g(), h(), i() and j(), in sequence, as shown below. If any of those fail with a "foo" or "bar" error, f() is to handle the error immediately then return successfully. If any other error occurs, f() is to propagate the error information back to the caller.Here is the code if exceptions are used:void f()  ← using exceptions{  try {    GResult gg = g();    HResult hh = h();    IResult ii = i();    JResult jj = j();    ...  }  catch (FooError& e) {    ...code that handles "foo" errors...  }  catch (BarError& e) {    ...code that handles "bar" errors...  }}The "good" path and the "bad" path are cleanly separated. The "good" (or "happy") path is the body of the try block — you can read that linearly, and if there are no errors, control flows in a simplistic path through those lines. The "bad" path is the body of the catch block and the body of any matching catch blocks in any caller.Using return codes instead of exception clutters this to the point where it is difficult to see the relatively simple algorithm. The "good" ("happy") and "bad" paths are hopelessly intermixed:int f()  ← using return-codes{  int rc;  //"rc" stands for "return code"  GResult gg = g(rc);  if (rc == FooError) {    ...code that handles "foo" errors...  } else if (rc == BarError) {    ...code that handles "bar" errors...  } else if (rc != Success) {    return rc;  }  HResult hh = h(rc);  if (rc == FooError) {    ...code that handles "foo" errors...  } else if (rc == BarError) {    ...code that handles "bar" errors...  } else if (rc != Success) {    return rc;  }  IResult ii = i(rc);  if (rc == FooError) {    ...code that handles "foo" errors...  } else if (rc == BarError) {    ...code that handles "bar" errors...  } else if (rc != Success) {    return rc;  }  JResult jj = j(rc);  if (rc == FooError) {    ...code that handles "foo" errors...  } else if (rc == BarError) {    ...code that handles "bar" errors...  } else if (rc != Success) {    return rc;  }  ...  return Success;}By intermixing the good/happy path with the bad/error path, it's harder to see what the code is supposed to do. Contrast that with the version that used exceptions, which is almost self-documenting — the basic functionality is very obvious. 

Browse random answers:

What is Exceptions in c++ ?
Can we generate a C++ source code from the binary file?
what is Un handled exception(KERNEL32.DLL):0xE06D7363 in the context of exception handling? pls answer to my mail id.in the flg code i got a problem.i want to know the entire steps followed in exception handling in this program.#include <iostream>using namespace std;int main(){ cout << "Start"; try  { // start a try block cout << "Inside try block"; cout << "Still inside try block"; throw 78; } catch (double i)  { // catch an error cout << "Caught an exception -- value is: "; cout << i << ""; } cout << "End"; return 0;}
How does throwing and catching exceptions differ from using setjmp and longjmp?
what is the difference betwen wait() and delay()?
How can I get around scope problems in a try/catch?
Explain exception specifications with example ?
Explain about standard exceptions with example ?
Explain about try, catch, and throw Statements (C++) ?
Exceptions are hard to ignore, unlike error codes. ?
What are C++ Exception  Examples ?
Explain about Unhandled C++ Exceptions ?
Explain about exception Handling Overhead ?
Explain about Mixing C (Structured) and C++ Exceptions ?
Explain about Overview of C++ Exception Handling with example ?
Explain about Implications of Using Exceptions with example ?
Explain about Designing With Exceptions with example ?
Explain about exceptions in Real-Time Systems ?
Explain about exception Handling Philosophies with example ?
Explain about exceptions in an Array-Based Stack ?
How many exceptions in a List-Based Stack ?
What are some ways try / catch / throw can improve software quality? 
How do exceptions simplify my function return type and parameter types? 
What does it mean that exceptions separate the "good path" (or "happy path") from the "bad path"? 
Exception handling seems to make my life more difficult; clearly I'm not the problem, am I?? 
How can I handle a destructor that fails? 
How can I handle a constructor that fails? 
How should I handle resources if my constructors may throw exceptions? 
What is exception-safe code? 
Explain about exception Neutrality ?
Explain how we implement exception handling in C++  
Explain terminate() and unexpected() function  ?
Exception handling concept  ?
Describe the way to handle run time error in C++.