File : a-elchha-zfp.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --    A D A . E X C E P T I O N S . L A S T _ C H A N C E _ H A N D L E R   --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 2012-2014, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  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/>.                                          --
  26 --                                                                          --
  27 -- GNAT was originally developed  by the GNAT team at  New York University. --
  28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 --  Default last chance handler for no propagation runtimes
  33 
  34 with Ada.Unchecked_Conversion;
  35 with System.Machine_Reset;
  36 
  37 with GNAT.IO; use GNAT.IO;
  38 --  We rely on GNAT packages for the output. Usually, Ada predefined units
  39 --  cannot depends on GNAT units, as the user could use the GNAT hierarchy.
  40 --  However, this implementation of Last_Chance_Handler is a default one, that
  41 --  could be redefined by the user.
  42 
  43 procedure Ada.Exceptions.Last_Chance_Handler
  44   (Msg : System.Address; Line : Integer)
  45 is
  46    procedure Put (Str : System.Address);
  47    --  Put for a nul-terminated string (a C string)
  48 
  49    ---------
  50    -- Put --
  51    ---------
  52 
  53    procedure Put (Str : System.Address) is
  54       type C_String_Ptr is access String (1 .. Positive'Last);
  55       function To_C_String_Ptr is new Ada.Unchecked_Conversion
  56         (System.Address, C_String_Ptr);
  57 
  58       Msg_Str : constant C_String_Ptr := To_C_String_Ptr (Str);
  59 
  60    begin
  61       for J in Msg_Str'Range loop
  62          exit when Msg_Str (J) = Character'Val (0);
  63          Put (Msg_Str (J));
  64       end loop;
  65    end Put;
  66 
  67 begin
  68    Put_Line ("In last chance handler");
  69 
  70    if Line /= 0 then
  71       Put ("Predefined exception raised at ");
  72       Put (Msg);
  73       Put (':');
  74       Put (Line);
  75    else
  76       Put ("User defined exception, message: ");
  77       Put (Msg);
  78    end if;
  79 
  80    New_Line;
  81 
  82    --  Stop the program
  83 
  84    System.Machine_Reset.Stop;
  85 end Ada.Exceptions.Last_Chance_Handler;