comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank" <franjoe@frisurf.no>
Subject: To raise an exception in task and handle/catch it in outer-block.
Date: Sat, 3 Jan 2004 21:58:08 -0800
Date: 2004-01-03T21:58:08-08:00	[thread overview]
Message-ID: <D3GJb.279$Mb5.5088@news2.e.nsc.no> (raw)

Hi!

What is the best practice for handling exceptions (by outer block) that are
raised in tasks?
My question is in particular, if the approach I have chosen in the example
below is recommended or not?

What I have tried, is that a task raises an exception. The outer block is
still waiting to perform "Stop" and "Final".
In this solution I have added two accepts for "Stop" and "Final" in the
exception-block of the task, to catch
the client-code's "Stop" and "Final".

What is the best way of handling this? To start testing "Is_Callable" or
similar clutters the code in my opinion.


Frank


package Test_Task_Exception is


   task type A_Task is
      entry Init( P_Fail : in Boolean) ;
      entry Start;
      entry Stop;
      entry Final;
   end A_Task;

end Test_Task_Exception;



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
           raise test_exception;
         end if;
      end loop;

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

   exception
      when others =>
        begin
          accept Stop do
            Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Stop -
Exception");
          end Stop;

          accept Final do
            Ada.Text_IO.Put_Line ("Test_Task_Exception.A_Task.Final -
Exception");
            raise test_exception;
          end Final;

        end;

   end A_Task;

end Test_Task_Exception;





with Test_Task_Exception;
use Test_Task_Exception;
with Text_IO;
use Text_IO;

procedure Test_Task_Main is
  type Pointer_To_A_Task is access Test_Task_Exception.A_Task;
  Task1, Task2 : Pointer_To_A_Task;
begin
  Put_Line("Main program");
  Task1 := new Test_Task_Exception.A_Task;
  Task2 := new Test_Task_Exception.A_Task;

  Put_Line("*************** Raise no exception");
  Task1.Init (False);
  Task1.Start;
  Task1.Stop;
  Task1.Final;

  Put_Line("*************** Do raise exception");
  Task2.Init (True);
  Task2.Start;
  Task2.Stop;
  Task2.Final;

end Test_Task_Main;







             reply	other threads:[~2004-01-04  5:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-04  5:58 Frank [this message]
2004-01-03 22:03 ` To raise an exception in task and handle/catch it in outer-block 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
replies disabled

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