0

I am using a Graphics Library called Irrlicht at some point i have to write this code

    if(!device){
       //error code here`
   }

i am not in the main function but want to close the application when this error happens please keep in mind that I am a beginner so this question might sound dumb i see some people do this:

int main(){
   if(!device){
      return 1;
   }
return 0;
}

i am not in the main function and want to exit the application outside of the main function

  • 1
    please explain your problem by more details – Pouya Imani Jul 11 '21 at 07:01
  • 1
    That is not "crashing" but terminating. So please change your headline! In c++ you can throw an exception and if it is currently not catched somewhere, the application will terminate. That has the advantage, that you can later on add an exception handler without modifying the rest of your code. If you like it simple, use `abort()` instead. – Klaus Jul 11 '21 at 07:05
  • use try-catch in c++ (read about it) or exit(0); exit(0) tells that the program ended normally. else you can use exit(-1); (ended with an error) and this is just an indicator. – Mohamed Akram Jul 11 '21 at 07:08
  • i have heard about the exit() function and i used it as a temporary solution but apparently people don't really like to use that function and say that it has problems – Bardia Bahrampour Jul 11 '21 at 07:12
  • 1
    Look up the `exit()` and `abort()` functions - both available by `#include `. If any functions in the call stack (including `main()`) can potentially recover from the error condition then consider throwing an exception - if the exception is not caught, the program will terminate, if it is caught then the catching code can either recover (e.g. by setting `device` to be something valid, and retrying the action that caused the error) or simply rethrow (which, again, will cause the program to terminate unless the exception is caught). – Peter Jul 11 '21 at 07:33

1 Answers1

0

The following example give you an idea about some of the possibilities.

You can simply copy and paste it and play around with it. Simply use only one line of the "termination actions" like throw or exit. If you don't have the try catch block in the main function, your application will also terminate because the exception will not be caught.

struct DeviceNotAvailable {}; 
struct SomeOtherError{};

void func()
{
    void* device = nullptr; // only for debug

    if (!device)
    {   
// use only ONE of the following lines:
        throw( DeviceNotAvailable{} );
        //throw( SomeOtherError{} );
        //abort();
        //exit(-1);
    }   
}

int main()
{
    // if you remove the try and catch, your app will terminate if you
    // throw somewhere
    try 
    {   
        func();
    }   
    catch(DeviceNotAvailable)
    {   
        std::cerr << "No device available" << std::endl;
    }   
    catch(SomeOtherError)
    {   
        std::cerr << "Some other error" << std::endl;
    }   

    std::cout << "normal termination" << std::endl;
}
Klaus
  • 24,205
  • 7
  • 58
  • 113