11.3 Raise Statements and Raise Expressions
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
raise_statement raises an exception.]
| raise exception_
raise_expression appears within the
expression of one of the following contexts, the
raise_expression shall appear within a pair of parentheses within the
expression. We need this restriction in order that
raise_expressions cannot be syntactically confused with immediately following constructs (such as
raise_expressionand the surrounding context; that's all we need to specify in order to eliminate the ambiguities. Moreover, we don't care at all where the left parenthesis is (so long as it is legal, of course).
Obj : Boolean := Func_Call or else raise TBD_Error with Atomic;
Obj : Boolean := Func_Call or else (raise TBD_Error) with Atomic;
Obj : Boolean := (Func_Call or else raise TBD_Error) with Atomic;
raise_expression, then both of the following are legal:
Obj : Boolean := Func_Call or else (raise TBD_Error with Atomic);
Obj : Boolean := (Func_Call or else raise TBD_Error with Atomic);
raise_expressions that are part of the "top-level" of an
expressionin one of the named contexts; the
raise_expressionis either the entire
expression, or part of a chain of logical operations. In practice, the
raise_expressionwill almost always be last in interesting top-level
expressions; anything that follows it could never be executed, so that should be rare. Other contexts such as conditional expressions, qualified expressions, aggregates, and even function calls, provide the needed parentheses. All of the following are legal, no additional parens are needed:
Pre : Boolean := (if not Is_Valid(Param) then raise Not_Valid_Error);
A : A_Tagged := (Some_Tagged'(raise TBD_Error) with Comp => 'A');
B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);
C : Natural := Func (Val => raise TBD_Error);
expressiondon't count. For instance, the parentheses around the
raise_expressionare required in the following:
D : A_Tagged := ((raise TBD_Error) with Comp => 'A');
ancestor_partis one of the contexts that triggers the rule.
initial_relation, which are the same as
choice_relationexcept for the inclusion of membership in
initial_expresioncould be used in place of
expressionin all of the contexts noted. We did not do that because of the large amount of change required, both to the grammar and to language rules that refer to the grammar. A complete grammar is given in .
raise_expressionis illegal in each of
decimal_fixed_point_definitionas these uses are required to be static and a
raise_expressionis never static. We include these in this rule so that Ada text has an unambiguous syntax in these cases.
name, if any, of a
raise_expression shall denote an exception. A
raise_statement with no exception_
name (that is, a re-raise statement) shall be within a handler, but not within a body enclosed by that handler.
Name Resolution Rules3.1/4
expression or string_
simple_expression, if any, of a
raise_expression is expected to be of type String.
The expected type for a
raise_expression shall be any single type.
To raise an exception is to raise a new occurrence of that exception[, as explained in 11.4]. For the execution of a
raise_statement with an exception_
name, the named exception is raised. Similarly, for the evaluation of a
raise_expression, the named exception is raised. [In both of these cases, if a string_
expression or string_
simple_expression is present, the expression is evaluated and its value is associated with the exception occurrence.] For the execution of a re-raise statement, the exception occurrence that caused transfer of control to the innermost enclosing handler is raised [again].
simple_expressionraises an exception, that exception is propagated instead of the one denoted by the exception_
Examples of raise statements:
raise Ada.IO_Exceptions.Name_Error; -- see A.13 raise Queue_Error with "Buffer Full"; -- see 9.11 8raise; -- re-raise the current exception 9/5-- For an example of a raise expression, see the Streams Subsystem definitions in 13.13.1.
Wording Changes from Ada 83
raise_statementhas to denote an exception is not clear from RM83. Clearly that was the intent, since the italicized part of the syntax rules so indicate, but there was no explicit rule. RM83-1.5(11) doesn't seem to give the italicized parts of the syntax any force.
Extensions to Ada 95
raise_statementis extended to include a string message. This is more convenient than calling Exceptions.Exception_Message (exception_
expression), and should encourage the use of message strings when raising exceptions.
Extensions to Ada 2012
raise_expressionis new. This construct is necessary to allow conversion of existing specifications to use preconditions and predicates without changing the exceptions raised. It is considered important enough to be added to Ada 2012 rather than waiting for Ada 2022.