comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank" <franjoe@frisurf.no>
Subject: Re: To raise an exception in task and handle/catch it in outer-block.
Date: Sat, 10 Jan 2004 13:05:26 -0800
Date: 2004-01-10T13:05:26-08:00	[thread overview]
Message-ID: <iWRLb.427$hd.12531@news2.e.nsc.no> (raw)
In-Reply-To: 17nuvvo1e7mjeca1jstfeud11o1gbv81jv@4ax.com

Hi!

Im dont understand your answer completely. What I get from it is that the
effect
of this kind of construction ..may be... platform-dependent, due to a
GNAT-bug (which is an OK answer).

I have tried (dont read it as "tested") the original example in Windows,
where it appeared to work
the 5-7 times I ran it :-)

It was my feeling that my construction was "agressive" this is why I check
it here.

1) I agree that there is a great chance of  locking tasks doing it in this
way.
2) The Text_IO is there for the moment for debugging purposes.
3) And by "outer-block" I mean the calling main program in my example.
4) Im currently using GNAT 3-15p on Windows XP.

This leaves me with the original "problem", how to I simplify the life of
the outer-block. In this posting a have noted two alternative.....

A)
--------------------------------
Norman H. Cohen's "Ada a Second language"(2nd ed) discusses this issue in
chapter
"18.9.1 Exceptions in Task Bodies", he proposes a solution where the
exception becomes
noticeable (but not "handled" when considering my example):

<Quote Norman H. Cohen>

task body My_Task
...
  use Ada.Text_IO; Ada.Task_Identification; Ada.Exception;
begin
..
exception
  when E: others => Put_Line(Current_Error, "Task " & Image(Current_Task) &
"terminated becuase of exception " &
       Exception_Name(E));
end My_Task;

</Quote Norman H. Cohen>

I have used it, and it works, but I was not quite satisifed because it still
leaves my outer-block in trouble....

B)
--------------------------------
A new theory I have now after reading this feedback, is to do the follwoing
(this is a rework of the example in the original posting):

What I do here is that I remove the "accept"'s in the exception-block of the
task and leaves an exceptions handling like
alternative (A) plus catching then "test_exception" in particular.
The accept is now moved to the if-statement where the
problem is found. Now the outer-block will
call these "alternative" accept-statements if the if-statement found a
problem. I leave the "raise test_exception" in the
"accept Final" as before, which should propagate to the outer-block.
(PS: Example has not been compiled by me)


Now I should be back on safe grounds again - or what?


with Ada.Text_IO;

package body Test_Task_Exception is

   test_exception : exception;

   task body A_Task is
     Run : Boolean := False;
     Counter : Integer := 0;
     Fail : Boolean;

   begin
      accept Init ( P_Fail : in Boolean) do
         Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Init - Body");
         Fail := P_Fail;
      end Init;

      accept Start do
         Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Start - Body");
         Run := True;
      end Start;

      while Run loop
         Counter := Counter + 1;

         Ada.Text_IO.Put_Line("Test_Task_Exception.A_Task.Task Working " &
Counter'Img);

         if Counter > 8 and not Fail then
            select
               accept Stop do
                  Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Stop -
Body");
                  Run := False;
               end Stop;

            else
                  null;
            end select;
         end if;

         if Counter > 8 and Fail then
            -- I wish to raise a exception and propagate it to the
outer-block.
            -- I add an accept here also that the outer-block will visit
now...
            accept Stop do
                Ada.Text_IO.Put_Line
("Test_Task_Exception.A_Task.Stop -Alternative handling");
                Run := False;
            end Stop;

            accept Final do
                Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Final - To
Raise exception");
                raise test_exception;
            end Final;
         end if;
      end loop;

      accept Final do
        Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Final - Body");
      end Final;

    exception
        when test_exception => null; -- short cut this one since it is
handled elsewhere.....

        when others =>
       -- "Norman H. Cohen's tick"

   end A_Task;

end Test_Task_Exception;







      parent reply	other threads:[~2004-01-10 21:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-04  5:58 To raise an exception in task and handle/catch it in outer-block Frank
2004-01-03 22:03 ` tmoran
2004-01-04 20:58   ` Frank
2004-01-04 20:35     ` tmoran
2004-01-07  3:48       ` Frank
2004-01-10  2:20     ` Craig Carey
2004-01-10  6:24       ` Robert I. Eachus
2004-01-10 21:05       ` Frank [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox