c语言中的异常处理_C中的错误处理

c语言中的异常处理

C language does not provide any direct support for error handling. However a few methods and variables defined in error.h header file can be used to point out error using the return statement in a function. In C language, a function returns -1 or NULL value in case of any error and a global variable errno is set with the error code. So the return value can be used to check error while programming.

C语言不提供任何直接的错误处理支持。 但是,可以在函数中使用return语句使用在error.h头文件中定义的一些方法和变量来指出错误。 在C语言中,如果发生任何错误,函数将返回-1NULL值,并使用错误代码设置全局变量errno 。 因此,返回值可用于在编程时检查错误。

什么是errno? (What is errno?)

Whenever a function call is made in C language, a variable named errno is associated with it. It is a global variable, which can be used to identify which type of error was encountered while function execution, based on its value. Below we have the list of Error numbers and what does they mean.

每当使用C语言进行函数调用时,就会将一个名为errno的变量与之关联。 它是一个全局变量,可用于根据函数的值来识别执行函数时遇到的错误类型。 下面是错误号的列表及其含义。

errno valueError
1Operation not permitted
2No such file or directory
3No such process
4Interrupted system call
5I/O error
6No such device or address
7Argument list too long
8Exec format error
9Bad file number
10No child processes
11Try again
12Out of memory
13Permission denied
错误值 错误
1个 不允许操作
2 无此文件或目录
3 没有这样的过程
4 系统调用中断
5 I / O错误
6 没有这样的设备或地址
7 参数列表过长
8 执行格式错误
9 档案编号错误
10 没有子进程
11 再试一次
12 记不清
13 没有权限

C language uses the following functions to represent error messages associated with errno:

C语言使用以下函数来表示与errno相关的错误消息:

  • perror(): returns the string passed to it along with the textual represention of the current errno value.

    perror() :返回传递给它的字符串以及当前errno值的文本表示形式。

  • strerror() is defined in string.h library. This method returns a pointer to the string representation of the current errno value.

    strerror()string.h库中定义。 此方法返回一个指向当前errno值的字符串表示形式的指针。

实例时间 (Time for an Example)

#include <stdio.h>       
#include <errno.h>       
#include <string.h> 
 
int main ()
{
    FILE *fp;
 
    /* 
        If a file, which does not exists, is opened,
        we will get an error
    */ 
    fp = fopen("IWillReturnError.txt", "r");
 
    printf("Value of errno: %d\n ", errno);
    printf("The error message is : %s\n", strerror(errno));
    perror("Message from perror");
 
    return 0;
}

Value of errno: 2 The error message is: No such file or directory Message from perror: No such file or directory

errno的值:2错误消息是:No such file or directory错误消息:没有此类文件或目录

错误处理的其他方式 (Other ways of Error Handling)

We can also use Exit Status constants in the exit() function to inform the calling function about the error. The two constant values available for use are EXIT_SUCCESS and EXIT_FAILURE. These are nothing but macros defined stdlib.h header file.

我们还可以在exit()函数中使用Exit Status常量来通知调用函数有关错误。 可以使用的两个常量值是EXIT_SUCCESSEXIT_FAILURE 。 这些只不过是宏定义的stdlib.h头文件。

#include <stdio.h>       
#include <errno.h>       
#include <stdlib.h>       
#include <string.h>       
 
extern int errno;
 
void main()
{
    char *ptr = malloc( 1000000000UL);  //requesting to allocate 1gb memory space
    if (ptr == NULL)    //if memory not available, it will return null 
    {  
        puts("malloc failed");
        puts(strerror(errno));
        exit(EXIT_FAILURE);     //exit status failure
    }
    else
    {
        free( ptr);
        exit(EXIT_SUCCESS);     //exit status Success      
    }
}

Here exit function is used to indicate exit status. Its always a good practice to exit a program with a exit status. EXIT_SUCCESS and EXIT_FAILURE are two macro used to show exit status. In case of program coming out after a successful operation EXIT_SUCCESS is used to show successful exit. It is defined as 0. EXIT_Failure is used in case of any failure in the program. It is defined as -1.

此处,退出功能用于指示退出状态。 退出具有退出状态的程序始终是一个好习惯。 EXIT_SUCCESSEXIT_FAILURE是用于显示退出状态的两个宏。 如果在成功操作后程序退出,则使用EXIT_SUCCESS表示成功退出。 定义为0。EXIT_Failure用于程序中发生任何故障的情况。 定义为-1。

被零除 (Division by Zero)

There are some situation where nothing can be done to handle the error. In C language one such situation is division by zero. All you can do is avoid doing this, becasue if you do so, C language is not able to understand what happened, and gives a runtime error.

在某些情况下,无法采取任何措施来处理错误。 在C语言中,这种情况之一就是被零除。 您所能做的就是避免这样做,因为如果这样做,C语言将无法理解发生了什么,并给出运行时错误。

Best way to avoid this is, to check the value of the divisor before using it in the division operations. You can use if condition, and if it is found to be zero, just display a message and return from the function.

避免这种情况的最佳方法是在除法运算中使用除数之前检查其值。 您可以使用if条件,并且如果发现它为零,则只需显示一条消息并从函数返回即可。

翻译自: https://www.studytonight.com/c/error-handling-in-c.php

c语言中的异常处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值