Ada:违反了"No_Exception_Propagation"

Ada: Violation of "No_Exception_Propagation"

在 LED_Demo STM32 示例项目中,我有以下代码(在 GPS 中编译):

procedure Does_Nothing is
begin
   Null;
exception
   when others =>
      Null;
end Does_Nothing;

如果我将相同的代码修改为下面的代码,它不会编译。我收到错误 "violation of restriction No_Exception_Propagation".

procedure Does_Nothing is
begin
   Null;
exception
   when Error: others =>
      UART.Put(Exception_Information(Error));
end Does_Nothing;

有人可以解释为什么会这样吗?

这在文档中有解释。

https://gcc.gnu.org/onlinedocs/gnat_rm/No_005fException_005fPropagation.html

(强调)

5.1.28 No_Exception_Propagation

[GNAT] This restriction guarantees that exceptions are never propagated to an outer subprogram scope. The only case in which an exception may be raised is when the handler is statically in the same subprogram, so that the effect of a raise is essentially like a goto statement. Any other raise statement (implicit or explicit) will be considered unhandled. Exception handlers are allowed, but may not contain an exception occurrence identifier (exception choice). In addition, use of the package GNAT.Current_Exception is not permitted, and reraise statements (raise with no operand) are not permitted.

标识符 Error 违反了限制。