test_exception.c

来自「c语言是面向过程的程序语言」· C语言 代码 · 共 59 行

C
59
字号
/* ****************************** * Object Oriented Programming in C * * Author: Laurent Deniau, Laurent.Deniau@cern.ch * * For more information, please see the paper: * http://home.cern.ch/ldeniau/html/oopc/oopc.html * ****************************** */#include <stdio.h>#include <math.h>#include <exception.h>/* can also be #defined */enum { no_exception, zero_divide, domain_error } exceptions;doublediv_(double a, double b){  if (b == 0.0) throw(zero_divide);  return a/b;}doubleln_(double a){  if (a <= 0.0) throw(domain_error);  return log(a);}intmain(int argc, char *argv[]){  double a, b;  if (argc!=3) return EXIT_FAILURE;  a = atof(argv[1]);  b = atof(argv[2]);  try {    printf("ln(%g/%g) = %g\n", a, b, ln_(div_(a,b)));  }  catch(zero_divide) {    printf("zero division\n");  }  catch(domain_error) {    printf("domain error\n");  }  catch_any {    printf("unknow exception no %d\n", exception);  }  endtry;  return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?