gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Exception handling within a compound statement?


From: Michael Riedl
Subject: Re: Exception handling within a compound statement?
Date: Wed, 20 Dec 2023 17:37:58 +0100
User-agent: Mozilla/5.0 (X11; Linux i686; rv:102.0) Gecko/20100101 Thunderbird/102.11.0

Phil,

here a simple sample from one of my libraries in the hope it will help ...

PROCEDURE CardPow(X : REAL;
                  N : CARDINAL) : REAL;

          VAR z : REAL;
BEGIN
      z:=1.0;
      LOOP
        IF ODD(N) THEN z:=z*X END; (* can give an overflow exception *)
        N:=N DIV 2;
        IF (N = 0) THEN EXIT; END;
        X:=X*X;                    (* can give an overflow exception *)
      END; (* LOOP *)
      RETURN z;
EXCEPT
      Errors.ErrOut("Overflow (LMathLib.CardPow)");
      RETURN INF;
END CardPow;

Errors.ErrOut is a simple routine displaying an error message on the standard error channel

and INF a predefined constant indicating IEEE positive infinity. The code is ISO M2 - PIM has no such features.

Gruß

Michael

Am 20.12.23 um 14:24 schrieb Gaius Mulley:
"Fischlin  Andreas" <andreas.fischlin@env.ethz.ch> writes:

Dear Phil,

Modula-2 has the philosophy of handling errors explicitly as the programer 
strives to write an error free code in the first place. The only exceptions 
that can
be considered are numerical ones, such as an integer or real overflow etc., 
which are detected and handled by the underlying hardware and system settings.
Depending on the current setting for the handling of such exceptions (tolerate 
or respond etc.) this results typically in a HALT of the Modula-2 program with
some information on the causes of the exception and/or triggering of a debugger.

Hope this helps.

Sincerely yours,
Andreas Fischlin

ETH Zurich
Prof. em. Dr. Andreas Fischlin
Systems Ecology - Institute of Biogeochemistry and Pollutant Dynamics
CHN E 24
Universitaetstrasse 16
8092 Zurich
SWITZERLAND

andreas.fischlin@env.ethz.ch
https://sysecol.ethz.ch/people/andreas.fischlin.html

+41 44 633-6090 phone
+41 79 595-4050 mobile

              Make it as simple as possible, but distrust it!
________________________________________________________________________

  On 20 Dec 2023, at 04:20, Philip Munts <phil@munts.net> wrote:

  Ada allows exception handling within each begin/end block, and
  also allows a begin/end block inside a compound statement:

  loop
   begin
     raise Program_Error;
   exception
     when Program_Error =>
       ...
   end;
  end loop;

  Does GM2 allow something similar, or can exceptions be caught
  only at the procedure or module level?

  I like to use exceptions to propagate errors in Ada embedded
  real-time software, since they can be caught and handled
  anywhere along the calling chain or just ignored to abort the
  program.

  If exceptions can only be caught at procedure or module level
  on the way out to a program abort, they are much less useful
  to me.

  Phil
Hi Andreas and Phil,

very true for pim[234] (I guess on some systems nil violation might be
caught by the underlying platform?).  In iso exceptions are allowed but
only in the begin end blocks of modules and procedures.

An interesting exception example from the iso standard can be found in:

    gcc/testsuite/gm2/iso/run/pass/except8.mod

    
http://cvs.savannah.nongnu.org/viewvc/gm2/testsuite/gm2/iso/run/pass/except8.mod?view=markup

(not that I'm advocating writing code like this :-).  On a more
practical note the exception handling scheme works well and across
languages - and if using swig across into scripting languages. (Python3
can catch a m2 exception).  In gm2 the procedure THROW is available from
the SYSTEM module and can be invoked from anywhere,

regards,
Gaius




reply via email to

[Prev in Thread] Current Thread [Next in Thread]