1 (* EXCEPTIONS.mod implement the ISO EXCEPTIONS specification.
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE EXCEPTIONS ;
34 FROM SYSTEM IMPORT ADR ;
35 FROM Storage IMPORT ALLOCATE ;
39 ExceptionSource = POINTER TO RECORD
40 eh: RTExceptions.EHBlock ;
42 (* values of this type are used within library modules to
43 identify the source of raised exceptions *)
46 PROCEDURE AllocateSource (VAR newSource: ExceptionSource) ;
47 (* Allocates a unique value of type ExceptionSource *)
51 eh := RTExceptions.InitExceptionBlock()
56 PROCEDURE RAISE (source: ExceptionSource;
57 number: ExceptionNumber;
58 message: ARRAY OF CHAR) ;
59 (* Associates the given values of source, number and message with
60 the current context and raises an exception.
63 RTExceptions.SetExceptionSource(source) ;
64 RTExceptions.SetExceptionBlock(source^.eh) ;
65 RTExceptions.Raise(number, ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__), ADR(message)) ;
66 (* we should never reach here as Raise should jump to the exception handler *)
67 M2RTS.Halt(__FILE__, __LINE__, __FUNCTION__, 'should never return from RTException.Raise')
71 PROCEDURE CurrentNumber (source: ExceptionSource) : ExceptionNumber ;
72 (* If the current coroutine is in the exceptional execution state
73 because of the raising of an exception from source, returns the
74 corresponding number, and otherwise raises an exception.
77 IF RTExceptions.IsInExceptionState()
79 RETURN( RTExceptions.GetNumber(source^.eh) )
81 RTExceptions.Raise(ORD(M2EXCEPTION.coException),
82 ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__),
83 ADR('current coroutine is not in the exceptional execution state'))
88 PROCEDURE GetMessage (VAR text: ARRAY OF CHAR) ;
89 (* If the current coroutine is in the exceptional execution state,
90 returns the possibly truncated string associated with the
91 current context. Otherwise, in normal execution state,
92 returns the empty string.
98 IF RTExceptions.IsInExceptionState()
102 p := RTExceptions.GetTextBuffer(RTExceptions.GetExceptionBlock()) ;
103 WHILE (p#NIL) AND (p^#ASCII.nul) DO
118 PROCEDURE IsCurrentSource (source: ExceptionSource) : BOOLEAN ;
119 (* If the current coroutine is in the exceptional execution state
120 because of the raising of an exception from source, returns TRUE,
121 and otherwise returns FALSE.
124 RETURN( RTExceptions.IsInExceptionState() AND (source=RTExceptions.GetExceptionSource()) )
125 END IsCurrentSource ;
128 PROCEDURE IsExceptionalExecution () : BOOLEAN ;
129 (* If the current coroutine is in the exceptional execution state
130 because of the raising of an exception, returns TRUE,
131 and otherwise returns FALSE.
134 RETURN( RTExceptions.IsInExceptionState() )
135 END IsExceptionalExecution ;