abort, terminate or exit?

后端 未结 6 581
心在旅途
心在旅途 2021-01-29 20:31

What\'s the difference between those three, and how shall I end program in case of exception which I can\'t handle properly?

6条回答
  •  春和景丽
    2021-01-29 20:42

    My advice would be not to use any of them. Instead, catch the exceptions you can't handle in main() and simply return from there. This means that you are guaranteed that stack unwinding happens correctly and all destructors are called. In other words:

    int main() {
        try {
           // your stuff
        }
        catch( ... ) {
           return 1;    // or whatever
        }
    }
    

提交回复
热议问题