1

In the LED_Demo STM32 example project I have the following code (which compiles in GPS):

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

If I modify the same code to the code below, it does not compile. I get the error "violation of restriction No_Exception_Propagation".

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

Could someone explain why this is the case?

John Leimon
  • 1,063
  • 1
  • 9
  • 13

1 Answers1

4

This is explained in the documentation.

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

(emphasis added)

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.

The identifier Error violates the restriction.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • "Exception handlers are allowed, but may not contain an exception occurrence identifier (exception choice)." Do you know the rationale for this? – John Leimon Jan 27 '17 at 22:05
  • @Johnson: No, I don't. Perhaps it could be used to do something nefarious with the exception occurrence, like passing it elsewhere, but my Ada is quite rusty. Or perhaps there's some technical reason that makes it easier to enforce that way. – Keith Thompson Jan 27 '17 at 22:08
  • My understanding of why this is disallowed when the `No_Exception_Propagation ` is used is that the functionality needed for storing and preserving information regarding the current exception needs to be implemented in the `Ada.Exceptions` package. Since certain hardware may not be able to support features like stack unwinding used to implement more comprehensive exception handling this pragma just restricts this capability entirely. My understanding, backed up by the GCC documentation, is that when this pragma is used all local exception handling is essentially the equivalent of `goto` in C. – ajxs Mar 09 '20 at 05:23