Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "bubble up the call stack" mean?

Tags:

c#

exception

I have watched a tutorial course on the Pluralsight website about Exception handling.instructor said

Error handling using error code we need to add if statements or switch statements to check the return codes. An error doesn't bubble up the call stack to higher-level code.

I didn't understand this sentence (error doesn't bubble up the call stack to higher-level code).

like image 627
Behnam Avatar asked Sep 17 '25 13:09

Behnam


1 Answers

It means that error codes returned by functions don't trigger anything unless explicitly handled by the caller.

Consider the following pseudo code defining a function:

 int f ()
 {
     ...
     if (/* some condition that indicates an error */) {
         return 1;
     }
     else {
         return 0;
     }
 }

If you now simply call the function f and an error occurred nothing will happen unless you explicitly check if the returned value indicates an error. Depending on the nature of the error, the program will probably continue on in an erroneous state.

Also any callers of the code that called the function won't be notified of that error unless it's explicitly returned to them. That what's meant with "bubbling up". To illustrate that, consider another function calling the first function without checking for errors:

int g ()
{
    ...
    f();
    ...
}

Now any caller calling g also won't get any indicator of the failure of f. That can be OK, if the error doesn't affect the calculations g does and neither the state of the program as a whole. But if the missing handling is due to a mistake (or laziness) of the programmer and does compromise the result of g (and possibly anything above it in the call stack or the whole program) no one will ever be notified.

With exceptions it's the other way around. Unless they are explicitly handled, they are handed from caller to caller, i.e. bubbling up the call stack -- think of air bubbles in water, they bubble up.

like image 197
sticky bit Avatar answered Sep 20 '25 04:09

sticky bit