1 (* EXCEPTIONS.mod implement the ISO EXCEPTIONS specification.
3 Copyright (C) 2003-2024 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('should never return from RTException.Raise',
68 __FILE__, __FUNCTION__, __LINE__)
72 PROCEDURE CurrentNumber (source: ExceptionSource) : ExceptionNumber ;
73 (* If the current coroutine is in the exceptional execution state
74 because of the raising of an exception from source, returns the
75 corresponding number, and otherwise raises an exception.
78 IF RTExceptions.IsInExceptionState()
80 RETURN( RTExceptions.GetNumber(source^.eh) )
82 RTExceptions.Raise(ORD(M2EXCEPTION.coException),
83 ADR(__FILE__), __LINE__, __COLUMN__, ADR(__FUNCTION__),
84 ADR('current coroutine is not in the exceptional execution state'))
89 PROCEDURE GetMessage (VAR text: ARRAY OF CHAR) ;
90 (* If the current coroutine is in the exceptional execution state,
91 returns the possibly truncated string associated with the
92 current context. Otherwise, in normal execution state,
93 returns the empty string.
99 IF RTExceptions.IsInExceptionState()
103 p := RTExceptions.GetTextBuffer(RTExceptions.GetExceptionBlock()) ;
104 WHILE (p#NIL) AND (p^#ASCII.nul) DO
119 PROCEDURE IsCurrentSource (source: ExceptionSource) : BOOLEAN ;
120 (* If the current coroutine is in the exceptional execution state
121 because of the raising of an exception from source, returns TRUE,
122 and otherwise returns FALSE.
125 RETURN( RTExceptions.IsInExceptionState() AND (source=RTExceptions.GetExceptionSource()) )
126 END IsCurrentSource ;
129 PROCEDURE IsExceptionalExecution () : BOOLEAN ;
130 (* If the current coroutine is in the exceptional execution state
131 because of the raising of an exception, returns TRUE,
132 and otherwise returns FALSE.
135 RETURN( RTExceptions.IsInExceptionState() )
136 END IsExceptionalExecution ;