/* * This is similar to trycatch.cpp, but it demonstrates that calling functions can catch exceptions * thrown by the function that it calls. * Author: Ying Li */ #include int childFunc () { int q; try { printf("Enter a number (0..9): "); int k = scanf("%d", &q); if (k == 0) { scanf("%*s"); throw "value is not a number"; } if (q < 0 || q > 9) { throw q; } } catch (int v) { printf("Error: number %d is out of range (0..9)\n", v); return v; } // catch (char *s) { // printf( "Other error: %s\n", s ); // } return q; } int main () { int val; for (;;) { try { val = childFunc(); } catch (const char *s) { printf("Error: %s\n", s); continue; } if (val == 0) break; } return 0; }