abortabort

中止当前进程,并返回错误代码。Aborts the current process and returns an error code.

备注

不要使用此方法关闭 Microsoft 应用商店应用或通用 Windows 平台 (UWP) 应用程序,除非在测试或调试方案。Do not use this method to shut down a Microsoft Store app or Universal Windows Platform (UWP) app, except in testing or debugging scenarios. 编程或 UI 方式关闭应用商店应用程序不允许根据Microsoft 存储策略Programmatic or UI ways to close a Store app are not permitted according to the Microsoft Store policies. 有关详细信息,请参阅UWP 应用生命周期For more information, see UWP app lifecycle.

语法Syntax

void abort( void );

返回值Return Value

中止不会将控制权返回到调用进程。abort does not return control to the calling process. 默认情况下,它会检查中止信号处理程序,并引发SIGABRT如果已设置。By default, it checks for an abort signal handler and raises SIGABRT if one is set. 然后中止终止当前进程,并返回到父进程的退出代码。Then abort terminates the current process and returns an exit code to the parent process.

备注Remarks

Microsoft 专用Microsoft Specific

默认情况下,当使用调试运行时库中,生成应用中止例程显示一条错误消息之前SIGABRT引发。By default, when an app is built with the debug runtime library, the abort routine displays an error message before SIGABRT is raised. 对于在控制台模式下运行的控制台应用程序,将消息发送到STDERRFor console apps running in console mode, the message is sent to STDERR. 以窗口模式运行的 Windows 桌面应用程序和控制台应用程序在消息框中显示此消息。Windows desktop apps and console apps running in windowed mode display the message in a message box. 若要禁止显示消息,使用_set_abort_behavior清除 _WRITE_ABORT_MSG标志。To suppress the message, use _set_abort_behavior to clear the _WRITE_ABORT_MSG flag. 所显示的消息取决于使用的运行时环境的版本。The message displayed depends on the version of the runtime environment used. 对于使用 Visual c + + 的最新版本生成的应用程序,该消息类似于此:For applications built by using the most recent versions of Visual C++, the message resembles this:

R6010-已调用 abort ()R6010 - abort() has been called

在以前版本的 C 运行时库中,该消息显示为:In previous versions of the C runtime library, this message was displayed:

This application has requested the Runtime to terminate it in an unusual way.This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.Please contact the application's support team for more information.

当程序在调试模式下编译时,消息框显示“中止”、“重试”或“忽略”选项。When the program is compiled in debug mode, the message box displays options to Abort, Retry, or Ignore. 如果用户选择“中止”,该程序立即终止并返回退出代码 3。If the user chooses Abort, the program terminates immediately and returns an exit code of 3. 如果用户选择“重试”,将调用调试器进行实时调试(如果可用)。If the user chooses Retry, a debugger is invoked for just-in-time debugging, if available. 如果用户选择忽略中止继续正常处理。If the user chooses Ignore, abort continues normal processing.

在零售和调试版本中,中止然后检查是否已设置的中止信号处理程序。In both retail and debug builds, abort then checks whether an abort signal handler is set. 如果设置非默认信号处理程序,中止调用raise(SIGABRT)If a non-default signal handler is set, abort calls raise(SIGABRT). 使用信号函数将使用的中止信号处理程序函数相关联SIGABRT信号。Use the signal function to associate an abort signal handler function with the SIGABRT signal. 你可以执行自定义操作,例如清除资源或日志信息,并在处理程序函数中使用自己的错误代码终止应用程序。You can perform custom actions—for example, clean up resources or log information—and terminate the app with your own error code in the handler function. 如果定义没有自定义的信号处理,则中止不会引发SIGABRT信号。If no custom signal handler is defined, abort does not raise the SIGABRT signal.

默认情况下,在非调试版本中的桌面或控制台应用,中止然后调用的 Windows 错误报告服务机制 (以前称为灾难恢复。Watson) 来向 Microsoft 报告故障。By default, in non-debug builds of desktop or console apps, abort then invokes the Windows Error Reporting Service mechanism (formerly known as Dr. Watson) to report failures to Microsoft. 此行为可以启用或禁用通过调用 _set_abort_behavior和设置也可能会掩盖 _CALL_REPORTFAULT标志。This behavior can be enabled or disabled by calling _set_abort_behavior and setting or masking the _CALL_REPORTFAULT flag. 设置该标志时,Windows 将显示一个消息框,其中包含类似“出现了一个问题,导致程序无法正常工作”的文本。When the flag is set, Windows displays a message box that has text something like "A problem caused the program to stop working correctly." 用户可以选择使用“调试”按钮调用调试器,或选择“关闭程序”按钮以终止带有操作系统定义的错误代码的应用程序。The user can choose to invoke a debugger with a Debug button, or choose the Close program button to terminate the app with an error code that's defined by the operating system.

如果不调用 Windows 错误报告处理程序,中止调用_exit终止的进程退出代码 3 并返回控制权移交给父进程或操作系统。If the Windows error reporting handler is not invoked, then abort calls _exit to terminate the process with exit code 3 and returns control to the parent process or the operating system. _exit不刷新流缓冲区或执行atexit/_onexit处理。_exit does not flush stream buffers or do atexit/_onexit processing.

有关 CRT 调试的详细信息,请参阅 CRT 调试技术For more information about CRT debugging, see CRT Debugging Techniques.

结束 Microsoft 专用End Microsoft Specific

要求Requirements

例程Routine 必需的标头Required header
abortabort <process.h> 或 <stdlib.h><process.h> or <stdlib.h>

示例Example

下面的程序尝试打开一个文件,如果该尝试失败,则中止。The following program tries to open a file and aborts if the attempt fails.

// crt_abort.c
// compile with: /TC
// This program demonstrates the use of
// the abort function by attempting to open a file
// and aborts if the attempt fails.

#include  <stdio.h>
#include  <stdlib.h>

int main( void )
{
    FILE    *stream = NULL;
    errno_t err = 0;

    err = fopen_s(&stream, "NOSUCHF.ILE", "r" );
    if ((err != 0) || (stream == NULL))
    {
        perror( "File could not be opened" );
        abort();
    }
    else
    {
        fclose( stream );
    }
}
File could not be opened: No such file or directory

请参阅See also

使用 abortUsing abort
abort 函数abort Function
进程和环境控制Process and Environment Control
_exec、_wexec 函数_exec, _wexec Functions
exit、_Exit、_exitexit, _Exit, _exit
raiseraise
signalsignal
_spawn、_wspawn 函数_spawn, _wspawn Functions
_DEBUG_DEBUG
_set_abort_behavior_set_abort_behavior