How to Use the abort, exit, atexit, and strerror Functions

Source: Internet
Author: User
Tags terminates

These are process control processes, not built-in language elements.

However, abort is defined in the sysutils unit:

The entire program is exited by throwing a silent exception.

Exit is defined in the system unit and used to exit the process (in exceptional cases, finally will still be executed)
In Delphi, the exit procedure immediately passes control away from the current procedure. If the current procedure is the main program, exit causes the program to terminate.

Note: Exit Passes control away from the current procedure, not merely the current block. but exit does not violate the flow of control dictated by a try .. finally construct; if exit is called inside the try clause, the finally clause is still executed.

Halt is also defined in the system unit. Exit the program and return a code to the operating system.
Procedure halt [(exitcode: integer)];

Error handling functions: abort, exit, atexit, strerror... C/C ++ 10:55:20 read 526 comments: 0 font size: large, medium, and small subscriptions.

Abstract: This article describes how to use several error handling functions, such as abort, exit, atexit, and strerror, and provides specific sample programs.

Function Name: Abort

Function: terminates a process abnormally.

Usage: void abort (void );

Header file: # include

Note: The abort function is a serious function. When it is called, it will cause the program to terminate abnormally,

Instead of clearing the memory, such as releasing the memory.

Program example:

# Include

# Include

Int main (void)

{

Puts ("about to abort.../N ");

Abort ();

Puts ("This will never be executed! /N ");

Exit (exit_success );

}

[Root @ localhost error_process] # GCC abort. c

[Root @ localhost error_process] #./A. Out

About to abort ....

Abandoned

-----------------------------------

Header file # include

Define the function void exit (INT status );

Exit () is used to terminate the execution of the current process normally and return the parameter status to the parent process,

All the buffer data of the process is automatically written back and closed.

It does not exit without any cleanup as abort does, but does not exit the program after all cleanup tasks are completed.

----------------------------------------

Atexit (set the function called before the program ends normally)

Header file # include

Define the function int atexit (void (* function) (void ));

If the returned value is successful, 0 is returned. Otherwise,-1 is returned. The cause of failure is stored in errno.

Atexit () is used to set a function called before the program ends normally. When the program calls exit () or returns a result from main, the function specified by the function parameter is called first, and then ends the program by exit.

Many times we need to do some operations such as releasing resources when the program exits, but there are many ways to exit the program, such as main () function execution ends, exit () is used to end the program somewhere in the program, and the user terminates the program through Ctrl + C or Ctrl + break, therefore, there must be a method unrelated to the program exit method to process the program exit. The method is to use the atexit () function to register the function to be called when the program ends normally.

The parameter of the atexit () function is a function pointer pointing to a function without parameters or returned values. The prototype of the atexit () function is: int atexit (void (*) (void ));

You can use atexit () to register up to 32 handler functions in a program. The Calling sequence of these handler functions is the opposite to that of their registration, that is, the last call of the first registration, the first call of the last registration.

 

# Include

# Include

Void my_exit (void)

{

Printf ("Before exit.../N ");

}

Int main (void)

{

Atexit (my_exit );

Return 0;

}

[Root @ localhost error_process] # GCC atexit. c

[Root @ localhost error_process] #./A. Out

Before exit ....

-----------------------------------------------------

Strerror (return the description string of the error cause)

Header file # include

Define the function char * strerror (INT errnum );

Strerror () is used to query the description string of the error cause based on the error code of the errnum parameter, and then return the string pointer.

In this case, if you pass a strerror to errno, you can get a readable prompt, instead of a cold number.

Return the string pointer that describes the cause of the error.

# Include

# Include

Int main (void)

{

Int I;

For (I = 0; I

{

Printf ("% d: % s/n", I, strerror (I ));

}

Return 0;

}

[Root @ localhost error_process] # GCC strerror. c

[Root @ localhost error_process] #./A. Out

0: Success

1: operation not permitted

2: no such file or directory

3: no such process

4: interrupted system call

5: Input/Output Error

6: no such device or address

7: argument list too long

8: exec format Error

9: Bad file descriptor

[Root @ localhost error_process] #

Http://hi.baidu.com/flyownway/blog/item/0cfb6703842d85ea08fa93b0.html

------------------------------------------------

3.9 handle errors

As we have seen, many system calls and functions described in this chapter will fail for various reasons. When a failure occurs, they set the value of the external variable errno to indicate the cause of the failure. Many different function libraries use this variable as a standard method for reporting errors. We also repeatedly warn you that the program must check the errno variable immediately after an error occurs in the function report, because it may be overwritten by the next function call, even if the next function has no error, this variable may also be overwritten.

The values and meanings of error codes are listed in the header file errno. H, including:

L eperm: The operation is not allowed.

L enoent: the file or directory does not exist.

L eintr: The system call is interrupted.

L EIO: I/O error.

L ebusy: the device or resource is busy.

L EEx ist: The file exists.

L einva L: Invalid parameter.

L emfile: too many open files.

L enodev: The device does not exist.

L eisdir: a directory.

L enotdir: Not a directory.

Two very useful functions can be used to report errors. They are strerror and perror.

3.9.1 strerror Function

The strerror function maps the error code to a string that describes the error types. This is useful when recording error conditions.

The function prototype is as follows:

 

3.9.2 perror Function

The perror function also maps the current error reported in the error variable to a string and outputs it to the standard error output stream. Add the information given by the parameter S (if S is not empty) before the string, and add a colon and a space.

The function prototype is as follows:

 

See the following example:

 

It will give the following output results in the standard error output:

 

Cancelupdates is a method inherited from the native ADO interface to cancel uncommitted records. Like cancelbatch (arall), native cancelbatch and cancelupdates are generally used only in batch mode, however, the single-record mode should be equally valid in the inheritance of tadoquery.

Cancel is a virtual method inherited from tdataset. It uses tcustomadodataset override, the parent class of tadoquery, to call the cancelupdate method of the native ADO interface and cancel the records currently edited or inserted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.