Environment.Exit(Int32) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Terminates this process and returns an exit code to the operating system.
public:
static void Exit(int exitCode);
public static void Exit (int exitCode);
static member Exit : int -> unit
Public Shared Sub Exit (exitCode As Integer)
- exitCode
- Int32
The exit code to return to the operating system. Use 0 (zero) to indicate that the process completed successfully.
The caller does not have sufficient security permission to perform this function.
For the exitCode
parameter, use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario. For example, return a value of 1 to indicate that the required file is not present, and a value of 2 to indicate that the file is in the wrong format. For a list of exit codes used by the Windows operating system, see System Error Codes in the Windows documentation.
Calling the Exit method differs from using your programming language's return
statement in the following ways:
Exit always terminates an application. Using the
return
statement may terminate an application only if it is used in the application entry point, such as in theMain
method.Exit terminates an application immediately, even if other threads are running. If the
return
statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminated.Exit requires the caller to have permission to call unmanaged code. The
return
statement does not.If Exit is called from a
try
orcatch
block, the code in anyfinally
block does not execute. If thereturn
statement is used, the code in thefinally
block does execute.If Exit is called when code in a constrained execution region (CER) is running, the CER will not complete execution. If the
return
statement is used, the CER completes execution.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.5, 1.6, 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: