Implementation of Exit( ) Function across various Platforms
The exit () function can be used across several platforms. However, the handling of their code is specific to the platform they are run on. Hence, we need to understand the manner in which different platforms handle the code so that we can learn to write the code in a better way.
We shall consider two commonly used platforms for this and go through their handling of the code one by one. The platforms are as follows:
- Windows
- Linux
Let us now understand the handling in detail.
Windows
On Windows, we can set the exit code to any 32-bit value. However, we reserve some of these values for reporting system errors such as crashes. So we can not use all values to set the exit code. Also, the dart executable itself uses 254 exit codes to report compile errors and 255 exit codes to report run-time errors (unhandled exceptions). Due to these facts, we recommend that we shall use exit codes in the range of 0-127 only to notify our environment of the results of running our dart program. We use these specified numbers so that we can avoid any issues while running the code across various platforms. This also allows our application to run smoothly.
Linux
On Linux and OS, the exit code for normal termination is always 0-255. If we set the exit code outside this range, the lower 8 bits are the actual exit code masked and treated as unsigned values. For example, using exit code 1 reports the actual exit code 255.
Implementation of the exit method:
We have learned numerous things about the dart exit () function. We discussed their syntax, usage, and dependency on various platforms. Now we shall try to understand implementing the exit function through a code.
Code:
void exit(int num) {
ArgumentError.checkNotNull(num, "num");
if (!_EmbedderConfig._mayExit) {
throw new UnsupportedError(
"This embedder disallows calling dart:io's exit()");
}
_ProcessUtils._exit(num);
}
Let’s use the exit function in an example to understand it better
Example:
In this example we will put a end statement before the next print statement so that only one print statement will be executed.
Code:
// Importing the dart:io package
import 'dart:io';
void main() {
print("This will be printed");
exit(0);
print("This will not be printed");
}
Output:
This will be printed
Frequently Asked Questions
Why do we use the dart exit() function?
We use the dart exit() function to terminate the running Dart VM and then exit the current program we are working on.
Which exit code indicates that the program has been successfully terminated?
Exit(0) indicates that the program has terminated successfully.
Is the handling of the dart exit function platform dependent or independent?
The dart exit() function are platform dependent.
What is the general range in which it is recommended to use the exit codes?
It is recommended to use exit codes in the range 0-127 to notify your environment of the results of running your dart program
Conclusion
In this article, we have extensively discussed the Exit function in Dart. We hope that this blog has helped you enhance your knowledge regarding Dart Exit Function, and to learn more, check out our other article on Dart Lists. And also check out the awesome content on the Coding Ninjas Website,
Android Development, Coding Ninjas Studio Problems, Coding Ninjas Studio Interview Bundle, Coding Ninjas Studio Interview Experiences, Coding Ninjas Courses, Coding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.
Happy Coding!