#include <stdio.h> #include <setjmp.h> //jmp_buf:陣列,儲存棧資訊即執行環境 jmp_buf buf; double Divide(double a, double b) { if (b == 0.0) { longjmp(buf, 1); // throw } else return a / b; } //setjmp儲存當前棧資訊,成功返回0,當執行到longjmp時, //恢復棧資訊即跳轉到setjmp位置重新執行setjmp //且此次返回值根據longjmp函式引數給定 int main(void) { int ret; ret = setjmp(buf); if (ret == 0) // try { printf("division ...\n"); printf("%f\n", Divide(5.0, 0.0)); } else if (ret == 1) // catch { printf("divisiong by zero\n"); } return 0; }
執行結果:
division ...
divisiong by zero
其實上次錯誤處理模式已經是c++異常處理雛形
上述相當於:
#include <iostream> using namespace std; double Divide(double a, double b) { if (b == 0.0) { throw 1; // throw } else return a / b; } int main(void) { try // try { cout<<"division ..."<<endl; cout<<Divide(3.0, 0.0)<<endl; cout<<Divide(5.0, 0.0)<<endl; } catch (int) // catch { cout<<"divisiong by zero"<<endl; } return 0; }
執行結果同上