Different Ways To Terminate C++ Program
A program must be terminated once its stated objectives have been met. Like any other language, C++ offers a variety of ways to terminate a program.
The following different methods will be discussed here to terminate a C++ program:
- abort() function
- terminate() function
- exit() function
Let’s start discussing each of these in detail.
abort function present inside a standard library header file. It terminates the program faster than other methods. The SIGABRT signal is raised by the abort function, which results in an abnormal termination of the current program. It is advised to utilize it only in the most extreme situations because it results in abnormal program termination.
The abort() function terminates the program without destroying the object and calling function passed to at_exit() or at_quick_exit() functions during the termination.
Parameter: This function doesn’t accept any parameter.
Return Value: This function doesn’t return any value.
Example 1: Below is the C++ program to abort the program after printing the welcome message.
Welcome To GeeksforGeeks ! Aborted
Explanation: In this program, the two print statements are used for two display messages and the abort function is called after one message. The abort () function is invoked when the first display message prints its output when the program gets executed. The program ends when the abort function is called. As a result, the sentence that follows the abort() method won’t be executed.
Example 2: Below is the C++ program for Abort() function in file handling.
No such file found Aborted
Explanation: This program checks if a given file is present or not. If the file is not present, then it will print the “not found” message and abort the program.
The exit() function is declared in the <stdlib.h>standard library header file. The exit function is used to exit or terminate the program. The Exit function exits a program while ignoring the rest of the code.
In other words, statements or codes after the exit function declaration are ignored by the system or will not be executed.
EXIT_SUCCESS and EXIT_FAILURE are constants that are defined in header files to indicate exit success or exit failure of programmer code.
Exit (value of exit);
Parameter: The exit function accepts a value as a Parameter. Every value except 0 indicates that the code was successfully terminated and every value except 0 indicates an error.
Return Value: The exit function doesn’t return a value.
Example 1: Below is the C++ program to implement exit():
Welcome To GeeksforGeek !
Explanation: The exit () function is invoked when the first display message prints its output. The program ends when the exit function is called. When the exit function is called, it ignores the statement code after this function. hence the sentence that follows the exit() method won’t be executed.
Example 2: Below is the C++ program to implement exit() to check whether the number is 0 or not.
Enter Number : 7 Valid input. The Number is : 7
Enter Number : 0 Error Occurred!
Explanation: When the user enters a positive value, it displays that value. However, if the user enters the value 0, this program will exit. and the statement following the exit() function is ignored by the system.
When the exception is thrown and not caught, When a constructor, destructor, static method, or thread throws an exception. Functions with the at_exit and at_quick_exit parameters raise an exception. An exception is thrown by the thread.
When the Terminate function is called in the program system, it calls the terminate_handler() function as well as the abort function. The terminate_handler() calls the abort function. The terminate function can be called directly by the program.
Parameter: It doesn’t accept any parameters.
Return Value: It doesn’t have a return type.
Below is the C++ program to implement terminate():
Abort signal from abort(3) (SIGABRT)
Output in Online Compiler:
Welcome To GeeksforGeeks ! terminate called without an active exception Aborted