How to Exit a Dart Application Unconditionally?
The exit() method exits the current program by terminating running Dart VM. This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is a similar exit in C/C++, Java. This method doesn’t wait for any asynchronous operations to terminate.
Syntax: exit(exit_code);
To use this method we have to import ‘dart:io’ package. The handling of exit codes is platform-specific.
- On Linux and OS, an exit code for normal termination will always be in the range of 0 to 255. If an exit code outside this range is set the actual exit code will be the lower 8 bits masked off and treated as an unsigned value. E.g. using an exit code of -1 will result in an actual exit code of 255 being reported.
- On Windows, the exit code can be set to any 32-bit value. However, some of these values are reserved for reporting system errors like crashes. Besides this, the Dart executable itself uses an exit code of 254 for reporting compile-time errors and an exit code of 255 for reporting runtime error (unhandled exception). Due to these facts, it is recommended to only use exit codes in the range 0 to 127 for communicating the result of running a Dart program to the surrounding environment. This will avoid any cross-platform issues.
Note: The exit(0) Generally used to indicate successful termination while rest generally indicates unsuccessful termination.
Implementation of the exit() method is as:
void exit(int code) { ArgumentError.checkNotNull(code, "code"); if (!_EmbedderConfig._mayExit) { throw new UnsupportedError( "This embedder disallows calling dart:io's exit()"); } _ProcessUtils._exit(code); }
Example: Using the exit() method to exit the program abruptly.
Dart
// Importing the packages import 'dart:io' ; // Main Function void main() { // This will be printed print( "Hello GeeksForGeeks" ); // Standard out code exit (0); // This will not be printed print( "Good Bye GeeksForGeeks" ); } |
Output:
Hello GeeksForGeeks
Please Login to comment...