| 1 |
/* Copyright (C) 2008-2025 Free Software Foundation, Inc. */ |
| 2 |
/* This file is part of GNU Modula-2. |
| 3 |
|
| 4 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free |
| 6 |
Software Foundation; either version 2, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 |
for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 16 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
// a c++ example of Modula-2 exception handling |
| 22 |
|
| 23 |
static int *ip = NULL; |
| 24 |
|
| 25 |
void fly (void) |
| 26 |
{ |
| 27 |
printf("fly main body\n") ; |
| 28 |
if (ip == NULL) |
| 29 |
throw; |
| 30 |
if (*ip == 0) |
| 31 |
throw; |
| 32 |
if (4 / (*ip) == 4) |
| 33 |
printf("yes it worked\n"); |
| 34 |
else |
| 35 |
printf("no it failed\n"); |
| 36 |
} |
| 37 |
|
| 38 |
/* |
| 39 |
* a CPP version of the Modula-2 example given in the ISO standard. |
| 40 |
* This is a hand translation of the equivalent except2.mod file in this |
| 41 |
* directory which is written to prove that the underlying CPP |
| 42 |
* runtime system will support ISO Modula-2 exceptions and to reinforce |
| 43 |
* my understanding of how the GCC trees are constructed and what |
| 44 |
* state is held where.. |
| 45 |
*/ |
| 46 |
|
| 47 |
void tryFlying (void) |
| 48 |
{ |
| 49 |
again_tryFlying: |
| 50 |
printf("tryFlying main body\n"); |
| 51 |
try { |
| 52 |
fly() ; |
| 53 |
} |
| 54 |
catch (...) { |
| 55 |
printf("inside tryFlying exception routine\n") ; |
| 56 |
if ((ip != NULL) && ((*ip) == 0)) { |
| 57 |
*ip = 1; |
| 58 |
// retry |
| 59 |
goto again_tryFlying; |
| 60 |
} |
| 61 |
printf("did't handle exception here so we will call the next exception routine\n") ; |
| 62 |
throw; // unhandled therefore call previous exception handler |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void keepFlying (void) |
| 67 |
{ |
| 68 |
again_keepFlying: |
| 69 |
printf("keepFlying main body\n") ; |
| 70 |
try { |
| 71 |
tryFlying(); |
| 72 |
} |
| 73 |
catch (...) { |
| 74 |
printf("inside keepFlying exception routine\n"); |
| 75 |
if (ip == NULL) { |
| 76 |
ip = (int *)malloc(sizeof(int)); |
| 77 |
*ip = 0; |
| 78 |
goto again_keepFlying; |
| 79 |
} |
| 80 |
throw; // unhandled therefore call previous exception handler |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
main () |
| 85 |
{ |
| 86 |
keepFlying(); |
| 87 |
printf("all done\n"); |
| 88 |
} |