Type Reason System.Security.SecurityException The immediate caller does not have the required permission.
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 tp://msdn.microsoft.com/library/ms681381.aspx in the Windows documentation.
Calling the Environment.Exit(int) method differs from using your programming language's return statement in the following ways:
Environment.Exit(int) 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 the Main method.
Environment.Exit(int) 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.
Environment.Exit(int) requires the caller to have permission to call unmanaged code. The return statement does not.
If Environment.Exit(int) is called from a try or finally block, the code in any catch block does not execute. If the return statement is used, the code in the catch block does execute.
Type | Reason |
---|---|
System.Security.Permissions.EnvironmentPermission | Requires permission to read environment variables. See System.Security.Permissions.EnvironmentPermissionAccess.Read. |
System.Security.Permissions.SecurityPermission | Requires unmanaged code permission. See System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode. |