I am just doing something at the moment in PHP, which is naturally
suited to managing via a "try,catch" kind of structure which just isn't
in PHP and means I am having to be very, very careful. I just
thought I might use it to put the case for some exceptions.

Basically, I have a whole bunch of tasks to run, called say "Task1",
"Task2" etc. So in an ideal world, they could run as

     RunTask1();
     RunTask2();
     RunTask3();
     ...

However, I can't let multiple instances try and run these tasks in tandem,
so I put a lock in place:

     GrabLock();
     RunTask1();
     RunTask2();
     RunTask3();
     ...
     ReleaseLock();

The nasty thing then comes in that if any task fails, I have to stop. So I
create a convention that every task returns "1" for success, "0" for
failure. Tasks can also be nested. So this gives me a structure like
this:

     if ( ! GrabLock() )
         return 0;

     $retval = 0;
     switch(0){ default:

         if ( ! RunTask1() )
             break;

         if ( ! RunTask2() )
             break;

         if ( ! RunTask3() )
             break;

         $retval = 1;
     }

     ReleaseLock();
     return $retval;

This works, but it is cumbersome, you have to be VERY careful and there are
other complications in terms of reporting sensible error messages when
tasks are shared.

In Python, the code would be written like this:

     try:
         GrabLock();
         try:
             RunTask1();
             RunTask2();
             RunTask3();
             ...
         finally:
              ReleaseLock(); # If GrabLock runs without raising any
                             # exceptions this will ALWAYS run
     except:
         # Gracefully handle any exceptions from "GrabLock", "RunTask1" etc...

which is an awful lot neater, and a lot safer, and can handle error
messages much more cleanly.


Anyway - this is just an example of why I think exceptions would be a really
cool thing to add to PHP.

Regs

Brian White








-------------------------
Brian White
Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy
Phone: +612-93197901
Web:   http://www.steptwo.com.au/
Email: [EMAIL PROTECTED]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to