C++自定义异常实例 发表于 2018-05-25 本文通过一个例子演示c++如何自定异常。 1234567891011121314151617181920212223242526272829#include <iostream>#include <exception>using namespace std;struct Div0Exception : public exception{ const char* what() const throw() { return "divisor is 0"; }};int divide(int a, int b){ if (b == 0) throw Div0Exception(); return a/b;}int main(void){ try { divide(10, 0); } catch (Div0Exception e) { cout<<e.what()<<endl; } cout<<"测试自定义异常"<<endl; return 0;}