tags:

views:

85

answers:

1

I was wondering if there was a way to prevent a while loop from prematurely erroring out or terminating. I've thrown a try/catch in there and it seems to keep terminating. (As to the cause why it's terminating, I'm still debugging).

    $stomp = $this->stomp;
    if(isset($queue) && strlen($queue) > 0) {
        error_log('Starting Monitor for: '.$queue);
        $stomp->subscribe($queue);

        while(true) {       
            $frame = $stomp->readFrame();
            if ($frame != null) { 
                // Callback must be an array: array('Class','Method);
                if(is_array($callback) && count($callback) == 2) {
                    try {
                        $body = $frame->body;                           
                        $callFunct = call_user_func_array($callback,array($body,$arguments));   
                        $stomp->ack($frame);                                
                    } catch(StompException $e) {
                        $msg = 'Stomp Monitor readFrame() Callback Fail: '.$e->getMessage();
                        $this->context->reportError('STOMP',array('errorDetails'=>$msg));
                    }
                } else {
                    error_log('Invalid Stomp Callback');
                }
            }
        }
    }   `

Thanks, Steve

A: 

There's nothing to break out of the loop, so while(true) will carry on until it hits a timeout or some form of error condition. As a fallback, it's worth setting either a break to break out of the loop on condition, or use a while condition that you can set to false;

while (true) {
   // do some things
   break;
}

or

$x = true;
while ($x) {
   // do some things
   $x = false;
}

that way, exit from the loop is under your control

However, timeouts and other fatal errors still terminate the script as normal If your code is breaking out of the while loop, you should be seeing some error, unless you have an error handler suppressing it

Mark Baker
Could you elaborate on the error handler you mentioned to suppress it? Something other than a try/catch?
Steve
PHP allows you to create custom error or exception handlers using the set_error_handler() and set_exception_handler() functions. These establish a callback that will be called whenever a non-fatal error occurs, or any exception that isn't trapped by a catch. These functions would typically be used for any custom error/exception handling, and can hide any problems in the code.
Mark Baker