Is it possible to use "go to" from the statement

All that has ever been told to me is to go to evil and stay away from them, but I think they can help me here (?). I would like to give the user the opportunity to restart the application when the exception is caught, and I had problems with moving my head around what to do ...

My application will be monitored by another process, but there are some exceptions when I want the user to be able to decide what to do without returning control to the calling process.

Is something like this “acceptable”? Any other suggestions?

Many thanks!

int main(){

    initialize:
       try{
        //do things
       }
       catch(...)
       {
          cout<<"Would you like to try initializing again?"<<endl;

          //if yes
            goto initialize;

          //if not
          abort(); //or something...
        }

return 0;
}
+5
source share
6 answers

Why not?

while(true){
  //Do stuff
  if(exit){
    break;
  }
}

or

continue = true;
do{
  //Do stuff
  if(exit){
    continue = false;
  }
}while(continue);
+7

:

int main()
{
     while(true)
     {
          try
          {
               program();
          }
          catch(std::exception& e)
          {
               std::cout << "Start again?" << std::endl;
               //Check ...
               if(!go_on)
                    break;
          }
     }
     return 0;
}
+5

A goto , .

, break a switch. continue , , , .

, .

(, " ?" ), ( "fail?" ) .

// Tries until successful, or user interaction demands failure.
bool initialize() {
    for ( ;; ) {
        try {
            // init code
            return true;
        }
        catch ( ... ) {
            cout << "Init Failed. Fail Program?" << endl;
            if ( yes ) {
                return false;
            }
        }
    }
}

int main() {
    if ( ! initialize() ) {
        return EXIT_FAILURE;
    }
    // rest of program
    return EXIT_SUCCESS;
}

: goto break, ( catch).

+5

, , "goto ".

+4

, - . , , , , goto.

, - :

void mainLoop() // get user settings, process, etc
{
   try
   {
     // 1) get user settings
     // 2) process data
     // 3) inform of the result
   }
   catch( const exception_type & e)
   {
     // inform of the error
   }      
}

int main()
{
  try
  {
    while(true)
      mainLoop();
  }
  catch(...)
  {
    std::cout<<"an unknown exception caught... aborting() " << std::endl;
  }
}
+1

( ) " goto ". Gotos , . , , , . , , - , . , , " " - @Justin - " goto?". , , , .

+1
source

All Articles