user3872616
user3872616

Reputation: 1

Stop program flow in the middle without using an exception

I need to stop the program flow in the middle, and I am currently using an exception for this. This flow is the legal flow and I want to know if I can do it without using an exception.

This is an example of my code, and I cannot change func_2 and func_1:

#include "stdio.h"

void func_3()
{
    printf("i am func_3\n");
    throw 20;
    printf("i am not supposed to be here\n");
}
void func_2()
{
    printf("i am func_2\n");
    func_3();
    printf("i am not supposed to be here\n");
}
void func_1()
{
    printf("i am func_1\n");
    func_2();
    printf("i am not supposed to be here\n");
}

int main()
{
    try
    {
        func_1();
    }
    catch (int e)
    {
        printf("i am supposed to be here\n");
    }
    catch (...)
    {
        printf("i am not supposed to be here\n");
    }
}

Upvotes: 0

Views: 431

Answers (3)

Sunil Hari
Sunil Hari

Reputation: 1776

Return can be used to return to the caller and stop the function being executed

 int GLOBAL_FLAG = 1;

function called_function(){
    printf("Inside Function")
    if(/*some condition*/)
         {
           GLOBAL_FLAG = 0;
           return;
         }
    /*Normal function code*/
}



  int main(){
    {
    called_function();
    if(GLOBAL_FLAG == 1)/*continue program execution*/
    printf("Function had been executed.Back to normal flow")
    }

So once the return statement is encountered it goes back to the caller that is main here and continues executing rest of the statements in main function.

Upvotes: 0

leemes
leemes

Reputation: 45675

I assume that you want to handle an exceptional case and are looking for an alternative to exceptions. I.e. I hope you don't want to continue with the program "normally" after handling your exceptional case, which is possible but not recommended to implement with exceptions.

Possible but not recommended alternatives to exceptions are:

  • When you want to stop your whole application, then you can use std::exit(0);. You can implement your "catch"-code in a function which you call instead of your "throw"-statement, and call std::exit(0); at the end of that function (or use another exit code to indicate an "unsuccessful" exit). Or you implement an exit handler and register it using std::atexit(&handle_exit);.

    Alternative to std::exit(<something>); is abort(); which throws the POSIX signal "SIGABRT" to indicate abnormal termination (which is the default behavior if your program throws and doesn't catch an exception). Your "catch"-code would then go in a signal handler which you register using the POSIX functions. Note that this requires a POSIX system and is thus not as portable as other solutions.

  • Another (similar) option is to use the "terminate" mechanism: Call std::terminate(); when you would normally throw your exception. Put your "catch"-code in a "terminate handler" function with signature void(*)(), i.e. no parameters and no return value, let's call the function void handle_terminate(). Install a terminate handler using std::set_terminate(&handle_terminate);. I didn't try that one, however, and it sounds damn ugly.

  • You could implement an exception-like behavior using assembly instructions, but please do not try this at home, as the behavior of such code is highly implementation defined (if not undefined), and way too ugly to implement.

Upvotes: 1

utnapistim
utnapistim

Reputation: 27365

In short, you can't (well ... you could, by using jumps instead, but then you would have two problems to solve).

The exception solution is the one to use, but do not throw a number (a number - especially a magical number in this case doesn't tell you anything).

Instead, define a struct func_3_interrupted {}; minimalistic structure, whose type name tells you it is an "interruption" of func_3, and catch that instead; The structure should be empty (or close to empty) and it should probably not inherit from the std::exception hierarchy.

Upvotes: 0

Related Questions