import math2; import c.stdio; import c.stdlib; class DivisionByZeroError: Error { this (char[] args){ super(args); } } double test_1 (double a, double b) in { assert(! (feq(b, 0.0))); } out (result) { assert(feq(result, a / b)); } body { return a / b; } double test_2 (double a, double b){ if (feq (b, 0.0)){ throw new DivisionByZeroError("Division by zero"); } return a / b; } int main (){ puts("Example for catching a contract violation"); fflush(stdout); try { double result = test_1(10.0, 0.0); } catch (Object a){ printf("You violated a preconditon of test_1\n"); } try { double result = test_2(10.0, 0.0); printf("result = %lf\n", result); } catch (DivisionByZeroError e){ e.print(); exit(EXIT_FAILURE); } return 0; }