送网站建设管理信息内容审核制度,嘉兴快速建站模板,网页设计旅游,网站设计主题目录
错误处理
异常处理
异常传播
异常规划
标准异常
自定异常 错误处理 在C语言中#xff0c;错误通常通过函数的返回值来表示。 错误返回值 对于能返回特殊值#xff08;如NULL或负值#xff09;的函数#xff0c;在调用时检查这些值来处理错误。 #include st…目录
错误处理
异常处理
异常传播
异常规划
标准异常
自定异常 错误处理 在C语言中错误通常通过函数的返回值来表示。 错误返回值 对于能返回特殊值如NULL或负值的函数在调用时检查这些值来处理错误。 #include stdio.h
#include stdlib.h#define DIV_RET_SUCCESS 1
#define DIV_RET_FAILED 0//C语言中通过函数返回值来判断执行成功与否
int myDiv(int num1, int num2, int* nRet)
{if (num2 0){*nRet - 0;return DIV_RET_FAILED;}*nRet num1 / num2;return DIV_RET_SUCCESS;
}int main()
{int ret 0;if (myDiv(10, 2, ret) DIV_RET_SUCCESS){printf(%d \r\n, ret);}return 0;
}错误码全局变量 如 errno这是一个全局变量很多标准库函数在出错时会设置这个变量为相应的错误码。 #pragma warning(disable:4996)
#include stdio.h
#include stdlib.h
#include errno.h
#include string.hint main()
{FILE* pFile NULL;pFile fopen(0xCC.txt, r);if (pFile NULL){printf(ErrorCode - %d \r\n, errno);printf(ErrorMesg - %s \r\n, strerror(errno));perror(fopen);}return 0;
}宏定义 通过宏定义可以创建简单的错误处理代码块这个方法提供了一种快速插入常用错误处理程序代码的方式。 #include stdio.h
#include stdlib.h
#define HANDLE_ERROR(msg) \do { \perror(msg); \exit(EXIT_FAILURE); \} while(0)
int main()
{FILE *fp fopen(file.txt, r);if (!fp) {HANDLE_ERROR(Error opening file);}// 其余代码...fclose(fp);return 0;
}异常处理 throw当问题发生时程序会抛出一个异常。这是通过 throw 关键字完成的后面跟着要抛出的异常对象。 throw ErrorCode
throw Erroetrytry 块内的代码是可能产生异常的代码当其中的代码抛出一个异常时执行流会跳转到匹配的 catch 块。 catchcatch 块会捕获异常并包含如何处理这些异常的代码。 try
{//可能抛出异常的代码
}
catch(ExceptionType var) //根据异常类型捕获
{//处理匹配异常类型
}
catch(ExceptionType var) //根据异常类型捕获
{//处理匹配异常类型
}示例代码 #include iostreamint AllocMem()
{//可能会抛出异常的代码放在try语句块内try{//throw A;long long* p new long long[0xFFFFFFF];//thorw bad_allocation}catch (int exception){std::cout exception std::endl;}catch (char exception){std::cout exception std::endl;}catch (std::exception exception){std::cout exception.what() std::endl;}}int main()
{AllocMem();return 0;
}异常传播 C异常传播Exception Propagation是指在程序中如果一个函数内发生了异常而该异常没有在该函数内得到处理则该异常会被传递到函数调用者处如果调用者也不处理则继续传递这样一直到最顶层调用者。如果最顶层调用者也没有处理异常则程序可能崩溃。 如果try块内的代码抛出了异常控制流会跳到第一个匹配的catch块。如果在当前函数中没有匹配的catch块异常会被传递给调用该函数的函数并在那里寻找匹配的catch块。如果在任一函数中都找不到匹配的catch块程序将调用terminate()结束程序。#include iostreamvoid Fun3()
{throw 0xCC;
}void Fun2()
{try{Fun3();}catch (char exception){std::cout exception std::endl;}}void Fun1()
{try{Fun2();}catch (float exception){std::cout Fun1 Exception std::endl;}}int main()
{try{Fun1();}catch (...){std::cout Main Exception std::endl;}return 0;
}异常规划
异常规范exception specification是C中的一种功能它允许开发者指明一个函数可能抛出哪些异常。自从C11起异常规范已经不再被推荐使用取而代之的是noexcept关键字。void someFunction() throw(int, char); // 只能抛出int型和char型异常void someFunction() noexcept; // 不会抛出异常 #include iostream
#include vector//该函数不会抛出任何异常
void Fun1() throw()
{}void Fun2() noexcept
{}void Fun3() throw(int)
{}void Fun4() throw(char)
{}void Fun5() throw(char, int)
{}int main()
{try{Fun3();}catch (int exception){std::cout exception std::endl;}try{Fun4();}catch (char exception){std::cout exception std::endl;}try{Fun5();}catch (char exception){std::cout exception std::endl;}catch (int exception){std::cout exception std::endl;}return 0;
}标准异常
std::exception 是各种标准异常类的基础提供了一个异常层次结构。 std::logic_error 逻辑错误表示程序的逻辑不当导致的问题这通常是可以预防的错误。 std::domain_error: 当一个数学函数接收到一个不在其定义域内的参数时抛出。std::invalid_argument: 当传递给函数的参数无效时抛出。std::length_error: 当创建过大的 std::string 或者 std::vector 时抛出。std::out_of_range: 当通过 at 访问 std::vector 或 std::string 而下标超出范围时抛出。 std::runtime_error 运行时错误表示在程序运行时发现的错误通常是难以预防的。 std::overflow_error: 当算术运算超过表示范围时抛出。std::underflow_error: 当算术运算结果在正确的表示范围内但非正规化时抛出。std::range_error: 当计算的结果不在可表示的范围内时抛出。std::system_error: 用于报告系统错误的异常。 std::bad_alloc 当动态内存分配失败时如 new 表达式std::bad_alloc 被抛出。 std::bad_cast 使用动态转换 (dynamic_cast) 转换到不兼容的类型时抛出 std::bad_cast。 std::bad_typeid 当使用类型信息功能如 typeid 操作符并且操作的对象是未定义类型时抛出 std::bad_typeid 异常。 std::bad_function_call 当调用一个空的 std::function 对象时抛出 std::bad_function_call 异常。 #include iostreamint main()
{try{long long* p new long long[0xFFFFFFF];}catch (const std::exception e){std::cout e.what() std::endl;}return 0;
}自定异常 #include iostreamclass myException :public std::exception
{
public:myException(std::string message): m_Message(message){}virtual const char* what() const{return m_Message.c_str();}private:std::string m_Message;
};int main()
{try{myException obj(cc exception);throw obj;}catch (const std::exception e){std::cout e.what() std::endl;}return 0;
}