   Using Goto, Why is it Such a Taboo?
  I have a game program, and if the initiations fails in any part of the program it needs to clean up all the resources and exit. This can't be done with breaks as it needs to jump to the end through several functions and then out of a loop.The perfect solution would be if failed, popup message and then goto the end of main and clean everything.Goto is not used for any other reason (not loops, switches, anything)But using goto is considered bad programming practice. Is this an exception, or is there a better way round it.Please note, if this goto gets hit in any function it will always be to end the application (to jump to the clean up and end naturally).Any comments welcome.JamesCobras   
  The scenario you described is pretty much what exceptions were invented for.   ;  There are cases when goto can be an elegant solution to a problem (although I've never had to resort to it)... however this is not one of them. This is what exceptions are for. Use them (assuming this is C++)   ;  You could also use RAII, that will make the resources fall out of scope.Quote:This can't be done with breaks as it needs to jump to the end through several functions and then out of a loop.The perfect solution would be if failed, popup message and then goto the end of main and clean everything.That sounds like something like using return values to indicate success.   ;  It's bad programming practice. Most of the time, use of Goto is an indication that the programmer does not 100% understand the inner workings of the routine he is working on.   ;Quote:Original post by MarminIt's bad programming practice. Most of the time, use of Goto is an indication that the programmer does not 100% understand the inner workings of the routine he is working on.Ok, what if you have a 5 deep nested loop? You could add to the conditions something like && breakOutOfLoop == false, but maybe a single goto would be cleaner?I've never needed to use it, and I would advise against it until you are experienced enough to know all other options available, but it might have it's ( limited ) uses.   ;  Hi guys, i need to skip any other functions if one fails and jump straight to the end.Using return values, means I'll need a load of if functions.Using goto, removes all the problems of breaking out of each function.This i think proves i understand how my program works.Does that rebut all the comments made?Source of main loop:int WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTR lpCmdLine,    int nCmdShow){    HWND hWnd;    WNDCLASSEX wc;    ZeroMemory(&wc, sizeof(WNDCLASSEX));    wc.cbSize = sizeof(WNDCLASSEX);    wc.style = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc = (WNDPROC)WindowProc;    wc.hInstance = hInstance;    wc.hCursor = LoadCursor(NULL, IDC_ARROW);    wc.lpszClassName = L"DirectxGameEngine";    RegisterClassEx(&wc);    hWnd = CreateWindowEx(NULL, L"DirectxGameEngine", L"DirectxGameEngine", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);    ShowWindow(hWnd, nCmdShow);    // set up and initialize Direct3D    initD3D(hWnd);//if failed (any of the initializations of class meshes jump to Clean)InitNx();//if failed (any of the initializations of class meshes jump to Clean)    // enter the main loop:    MSG msg;    while(TRUE)    {   DWORD starting_point = GetTickCount();   if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))   {  if (msg.message == WM_QUIT) break;  TranslateMessage(&msg);  DispatchMessage(&msg);   }   render_frame();   // check the 'escape' key   if(KEY_DOWN(VK_ESCAPE))  PostMessage(hWnd, WM_DESTROY, 0, 0);   while ((GetTickCount() - starting_point) < 25);    }    // clean up DirectX and COMClean: //where to jump toReleaseNx();    cleanD3D();    return msg.wParam;};  Like the others have said; this is exactly what exceptions are for.If you encounter an exceptional situation and must exit, throw an exception. In WinMain, you can catch the exception and do your cleanup. Additionally, you can use RAII so that cleanup is automatically performed when things go out of scope - eliminating most of the need for dedicated "cleaning" functions.Clean, idiomatic, and without goto.   ;Quote:Using goto, removes all the problems of breaking out of each function.Goto does not let you break out of a function. To break out of a function, you have to use 'return' or 'throw'.;  10 PRINT "Hello"20 GOTO 10HelloHelloHelloHelloHelloThose were the days...   