[PHP] Why I wish PHP had Python's exception handling....

2001-10-21 Thread Brian White


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]




Re: [PHP] Why I wish PHP had Python's exception handling....

2001-10-21 Thread Matt McClanahan

On Mon, Oct 22, 2001 at 02:57:44PM +1000, Brian White wrote:

Preface to these ramblings: Exception handling is planned for the next
version of the Zend engine.  For details, see

http://www.zend.com/zend/future.php

 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.

This is probably no less messy, but it does allow you to handle an
arbitrary number of steps, possibly with arguments, without having to
add another if check as you're doing above.  This also lets you know
easilly which step failed:

?

function first($a)  { if ($a) return true; else return false; }
function second($a) { if ($a) return true; else return false; }
function third($a)  { if ($a) return true; else return false; }

function GrabLock() { echo Grabbing.\n; return true; }
function ReleaseLock() { echo Releasing.\n; return true; }

$ret = true;
$funcs = Array('first','second','third');
$args = Array(1,1,1);   // Change one to zero to trigger a failure

if (!GrabLock())
return 0;   

while ($ret = $funcs[0]($args[0]))
{
array_shift($funcs);
array_shift($args); 
if (!count($funcs)) break;
}
 
ReleaseLock();

if (count($funcs)) echo Failed at $funcs[0].\n;

?

Matt

-- 
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]




Re: [PHP] Why I wish PHP had Python's exception handling....

2001-10-21 Thread Brian White

If that happens it would be great.


At 00:33 22/10/2001 -0500, Matt McClanahan wrote:
On Mon, Oct 22, 2001 at 02:57:44PM +1000, Brian White wrote:

Preface to these ramblings: Exception handling is planned for the next
version of the Zend engine.  For details, see

http://www.zend.com/zend/future.php

  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.

This is probably no less messy, but it does allow you to handle an
arbitrary number of steps, possibly with arguments, without having to
add another if check as you're doing above.  This also lets you know
easilly which step failed:

?

function first($a)  { if ($a) return true; else return false; }
function second($a) { if ($a) return true; else return false; }
function third($a)  { if ($a) return true; else return false; }

function GrabLock() { echo Grabbing.\n; return true; }
function ReleaseLock() { echo Releasing.\n; return true; }

$ret = true;
$funcs = Array('first','second','third');
$args = Array(1,1,1);   // Change one to zero to trigger a failure

if (!GrabLock())
 return 0;

while ($ret = $funcs[0]($args[0]))
{
 array_shift($funcs);
 array_shift($args);
 if (!count($funcs)) break;
}

ReleaseLock();

if (count($funcs)) echo Failed at $funcs[0].\n;

?

Matt

--
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]

-
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]