Open In App

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.



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.




// 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
Article Tags :