title: Ada: throwing wrong exception when opening a non-existent file
body: <p>This is a follow up of this [post] <a href="https://stackoverflow.com/questions/9794485/ada-adding-an-exception-in-a-separate-procedure-when-reading-a-file">Ada: Adding an exception in a separate procedure when reading a file</a></p>
  
  <p>When my code below opens a file that doesn't exist, instead of throwing a <code>ADA.IO_EXCEPTIONS.NAME_ERROR</code>, it is throwing a <code>ADA.IO_EXCEPTIONS.STATUS_ERROR</code>.</p>
  
  <p>Below are the codes.</p>
  
  <p>Main file: <code>test_read.adb</code>:</p>
  
  <pre><code>with Ada.Text_IO; use Ada.Text_IO;
  with Ada.Long_Float_Text_IO;
  with Ada.Float_Text_IO;
  
  
  procedure Test_Read is
  
     Input_File    : File_Type;
     value         : Long_Float;
  
     procedure Open_Data (File : in out  Ada.Text_IO.File_Type; Name : in String) is separate;
  
  begin
  
     Open_Data (File =&gt; Input_File, Name =&gt; "fx.txt");
  
     while not End_Of_File (Input_File) loop
        Ada.Long_Float_Text_IO.Get (File =&gt; Input_File, Item =&gt; value);
        Ada.Long_Float_Text_IO.Put (Item =&gt; value, Fore =&gt; 3, Aft  =&gt; 5, Exp  =&gt; 0);
     Ada.Text_IO.New_Line;
     end loop;
      Ada.Text_IO.Close (File =&gt; Input_File);
  
  end Test_Read;
  </code></pre>
  
  <p>And the <code>separate</code> procedure body <code>test_read-open_data.adb</code>:</p>
  
  <pre><code>separate (test_read)
  
  procedure Open_Data (File : in out  Ada.Text_IO.File_Type;
                   Name : in String) is
  
     --this procedure prepares a file for reading
     begin
        begin
           Ada.Text_IO.Open
             (File =&gt; File,
              Mode =&gt; Ada.Text_IO.In_File,
              Name =&gt; Name);
        exception
           when Ada.Text_IO.Name_Error =&gt;
           Ada.Text_IO.Put(File =&gt; Standard_Error, Item =&gt; "****File not found....****");
     end;
  end Open_Data;
  </code></pre>
  
  <p>The <em>error message</em> that is thrown:</p>
  
  <pre><code>****File not found....****
  
  Execution terminated by unhandled exception
  Exception name: ADA.IO_EXCEPTIONS.STATUS_ERROR
  Message: file not open
  Call stack traceback locations:
  0x40bf0f 0x40ead0 0x424b08 0x424e4a 0x4010b4 0x401146 0x7c817075
  [2012-03-21 13:45:44] process exited with status 1 (elapsed time: 00.13s)
  </code></pre>
  
  <p>I have tried a few things:</p>
  
  <ol>
  <li><p>Put <code>with Ada.IO_Exceptions;</code> in <code>test_read.adb</code> and put <code>when Ada.IO_Exceptions.Name_Error =&gt;</code> instead of <code>when Ada.Text_IO.Name_Error =&gt;</code> in <code>test_read-open_data.adb</code>. But there is no change.</p></li>
  <li><p>In <code>test_read-open_data.adb</code>, I changed the line <code>Mode =&gt; Ada.Text_IO.In_File</code> to make it become <code>Mode =&gt; Ada.Text_IO.Out_File</code> as shown below:</p>
  
  <p>Ada.Text_IO.Open
                 (File => File,
                  Mode => Ada.Text_IO.Out_File,
                  Name => Name);</p></li>
  </ol>
  
  <p>But there is no improvement.</p>
  
  <p>So how to get the <strong>right</strong> <code>ADA.IO_EXCEPTIONS.NAME_ERROR</code>?</p>
  
  <p><strong>A related question:</strong></p>
  
  <p>What's the difference between <code>Ada.Text_IO.Name_Error</code> and <code>ADA.IO_EXCEPTIONS.NAME_ERROR</code>? In my program I want to be informed when the file of name <code>fx.txt</code> does not exist. Which one of these two exceptions is appropriate?</p>
  
  <p>Thanks a lot</p>
  
  <p><strong>UPDATE (Latest Codes)</strong></p>
  
  <p>Here are my updated codes:</p>
  
  <pre><code>with Ada.Text_IO; use Ada.Text_IO;
  with Ada.Long_Float_Text_IO;
  with Ada.Float_Text_IO;
  
  
  procedure Test_Read is
  
     Input_File    : File_Type;
     value         : Long_Float;
     Success       : Boolean;
  
  procedure Open_Data (File : in out  Ada.Text_IO.File_Type; Name : in String; Success : out Boolean) is separate;
  
  begin
  
     Open_Data (File =&gt; Input_File, Name =&gt; "fx.txt", Success =&gt; Success);
     if not Success then
        return;
     end if;
  
     while not End_Of_File (Input_File) loop
        Ada.Long_Float_Text_IO.Get (File =&gt; Input_File, Item =&gt; value);
        Ada.Long_Float_Text_IO.Put (Item =&gt; value, Fore =&gt; 3, Aft  =&gt; 5, Exp  =&gt; 0);
        Ada.Text_IO.New_Line;
     end loop;
     Ada.Text_IO.Close (File =&gt; Input_File);
     Ada.Text_IO.Put_Line (Item =&gt; "Reading file success: " &amp; Boolean'Image (Success));
  
  end Test_Read;
  </code></pre>
  
  <p>and the <code>separate</code> procedure:</p>
  
  <pre><code>separate (test_read)
  
  procedure Open_Data (File : in out  Ada.Text_IO.File_Type;
                   Name : in String; Success : out Boolean) is
  
     --this procedure prepares a file for reading
       begin
        Success := True;
        begin
           Ada.Text_IO.Open
             (File =&gt; File,
              Mode =&gt; Ada.Text_IO.Out_File,
              Name =&gt; Name);
        exception
           when Ada.Text_IO.Name_Error  =&gt;
           Success := False;   
           Ada.Text_IO.Put (File =&gt; Standard_Error, Item =&gt; "****File not found....****");
        end;
  end Open_Data;
  </code></pre>
  
  <p>On compiling <strong>with file fx.txt present</strong>, I get:</p>
  
  <pre><code>Execution terminated by unhandled exception
  Exception name: ADA.IO_EXCEPTIONS.MODE_ERROR
  Message: file not readable
  Call stack traceback locations:
  0x40bf2b 0x40ead0 0x424b58 0x424f5a 0x4010b4 0x401146 0x7c817075
  [2012-03-22 08:39:39] process exited with status 1 (elapsed time: 00.14s)
  </code></pre>
  
  <p><strong>And the contents of the file <code>fx.txt</code> **get erased</strong> and its contents are not displayed on running the program**</p>
  
  <p>On compiling <strong>with file fx.txt renamed to fy.txt</strong>, I get:</p>
  
  <pre><code>****File not found....****
  [2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)
  </code></pre>
  
  <p>So what went wrong?</p>