Table of contents
1.
Introduction
2.
Syntax and Usage
3.
Implementation of Exit( ) Function across various Platforms
3.1.
Windows
3.2.
Linux
4.
Implementation of the exit method:
5.
Frequently Asked Questions
5.1.
Why do we use the dart exit() function?
5.2.
Which exit code indicates that the program has been successfully terminated?
5.3.
Is the handling of the dart exit function platform dependent or independent?
5.4.
What is the general range in which it is recommended to use the exit codes?
6.
Conclusion
Last Updated: Mar 27, 2024

Exit Function in Dart

Author Vivek Goswami
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Imagine the process of building up a website or building anything. So, what might be the process that strikes your head at once. If we neglect the intricacies of the procedure and outline the basics, it shall simply break down into three main steps:

  1. Enter the dart module
  2. Write your code
  3. Exit 

We have explored a multitude of topics on dart programming till now. Let us now develop our understanding of the dart exit function. As the name suggests, the function helps us in exiting the current program we are working on currently. 

Let us dive deep into the other aspects of the function now. 

Syntax and Usage

We use the dart exit() function to terminate the running Dart VM and then exit the current program we are working on. The method is assigned a status code. If the status code is negative, it is indicative of some abnormal termination. It is similar to the exit in other programming languages like C, C++, and Java.

In this method of exiting the program, we do not wait for any asynchronous operation to end that is currently active. Importation of ‘dart::io’ package is necessary so that we can use this method. 

The syntax is as follows:

exit (exit_code);

We need to note here that Exit(0) indicates successful termination. All other exit codes indicate an unsuccessful termination of the current program. 

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:

  1. Windows
  2. 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 DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

 

Live masterclass