|
11、出错信息的处理
—————————
你会处理出错信息吗?哦,它并不是简单的输出。看下面的示例:
if ( p == NULL ){
printf ( "ERR: The pointer is NULL\n" );
}
告别学生时代的编程吧。这种编程很不利于维护和管理,出错信息或是提示信息,应该统
一处理,而不是像上面这样,写成一个“硬编码”。第10条对这方面的处理做了一部分说
明。如果要管理错误信息,那就要有以下的处理:
/* 声明出错代码 */
#define ERR_NO_ERROR 0 /* No error */
#define ERR_OPEN_FILE 1 /* Open file error */
#define ERR_SEND_MESG 2 /* sending a message error */
#define ERR_BAD_ARGS 3 /* Bad arguments */
#define ERR_MEM_NONE 4 /* Memeroy is not enough */
#define ERR_SERV_DOWN 5 /* Service down try later */
#define ERR_UNKNOW_INFO 6 /* Unknow information */
#define ERR_SOCKET_ERR 7 /* Socket operation failed */
#define ERR_PERMISSION 8 /* Permission denied */
#define ERR_BAD_FORMAT 9 /* Bad configuration file */
#define ERR_TIME_OUT 10 /* Communication time out */
/* 声明出错信息 */
char* errmsg[] = {
/* 0 */ "No error",
/* 1 */ "Open file error",
/* 2 */ "Failed in sending/receiving a message",
/* 3 */ "Bad arguments",
/* 4 */ "Memeroy is not enough",
/* 5 */ "Service is down; try later",
/* 6 */ "Unknow information",
/* 7 */ "A socket operation has failed",
/* 8 */ "Permission denied",
/* 9 */ "Bad configuration file format",
/* 10 */ "Communication time out",
/* 10 */ "Communication time out",
};
/* 声明错误代码全局变量 */
long errno = 0;
/* 打印出错信息函数 */
void perror( char* info)
{
if ( info ){
printf("%s: %s\n", info, errmsg[errno] );
return;
}
printf("Error: %s\n", errmsg[errno] );
}
这个基本上是ANSI的错误处理实现细节了,于是当你程序中有错误时你就可以这样处理:
bool CheckPermission( char* userName )
{
if ( strcpy(userName, "root" != 0 ){
errno = ERR_PERMISSION_DENIED;
return (FALSE);
}
...
}
main()
{
...
if (! CheckPermission( username ) ){
perror("main()" ;
}
...
}
一个即有共性,也有个性的错误信息处理,这样做有利同种错误出一样的信息,统一用户
界面,而不会因为文件打开失败,A程序员出一个信息,B程序员又出一个信息。而且这样
做,非常容易维护。代码也易读。
当然,物极必反,也没有必要把所有的输出都放到errmsg中,抽取比较重要的出错信息或
是提示信息是其关键,但即使这样,这也包括了大多数的信息。 |
|