Help with better exception handling

We’re going through some code written by a contractor a couple yrs ago and adding better exception handling. This is an example of several functions that make SOAP calls to our webservice.
BTW, this code sometimes throws the error: Undefined index: result…Line 137 (marked below)


function ParticipantSelectByUser() {
  
  $param = array();
  $param["Username"] = $_POST["Username"];
  $param["Password"] = $_POST["Password"];
    
  $result = $this->client->call("ParticipantSelectByUser",array("parameters" => $param),"http://tempuri.org/");
  if ($this->client->fault) {
    $this->return_data["error"] .= print_r($result, true);
  } else {
    // Check for errors
    $err = $this->client->getError();
    if ($err) {
      // Display the error
      $this->return_data["error"] .= $err;
      $this->return_data["error"] .=  print_r($this->client->response, true);
      return json_encode($this->return_data);
    } else {
      $this->return_data["result"] = $result["ParticipantSelectByUserResult"];
    } // if ($err)
  } // if ($this->client->fault)
  return json_encode(array("result" => $this->return_data["result"])); // Line 137
} // function ParticipantSelectByUser()

Does this code below do a better job at handling error conditions than the original code above?


function ParticipantSelectByUser() {
  
  $param = array();
  $param["Username"] = $_POST["Username"];
  $param["Password"] = $_POST["Password"];

  try {    
    $result = $this->client->call("ParticipantSelectByUser",array("parameters" => $param), "http://tempuri.org/");
    if ($this->client->fault) {
      throw new Exception(print_r($result, true));
    } else {
      // Check for errors
      $err = $this->client->getError();
      if ($err) {
        // Display the error
        $this->return_data["error"] .= $err;
        $this->return_data["error"] .=  print_r($this->client->response, true);
        throw new Exception(json_encode($this->return_data));
      } // if ($err)
    } // if ($this->client->fault)
  } catch (Exception $e) {
    return json_encode($e);
  } // try..catch
  $this->return_data["result"] = $result["ParticipantSelectByUserResult"];
  return json_encode(array("result" => $this->return_data["result"]));
} // function ParticipantSelectByUser()

Hi

Yes your bottom example is better exception handling than the original.

I would though want to first understand why you are running into an undefined index; likely meaning that a null value is set for return_data[“result”]. You should not be getting data back that creates a null array index. So I would fix that first and only try to catch exceptions that you can’t control.

Regards,
Steve

Your try/catch is all wrong. The try/catch goes outside of the function/method…When you are using the object or function is where you use the try/catch.


try {
  $o = new Object();
  $o->ThrowException();
} catch ( Exception $e ) {
  // ...
}

How I would write the above method:


function ParticipantSelectByUser ()
{
  $param = array();
  $param["Username"] = $_POST["Username"];
  $param["Password"] = $_POST["Password"];

  $result = $this->client->call( "ParticipantSelectByUser",
    array( "parameters" => $param ), "http://tempuri.org/" );

  if ( $this->client->fault )
    throw new Exception( print_r( $result, true ) );

  // Check for errors
  $err = $this->client->getError();
  if ( $err ) {
    // Display the error
    $this->return_data["error"] .= $err;
    $this->return_data["error"] .=  print_r( $this->client->response, true );
    throw new Exception(json_encode($this->return_data));
  }

  $this->return_data["result"] = $result["ParticipantSelectByUserResult"];
  return json_encode( array( "result" => $this->return_data["result"] ) );
}

@logic_earth:
I see no try…catch in your reworking of my code. Also, when the Exception is caught, it won’t return the json-encoded result.

@ServerStorm:
I get the undefined Index because it falls through if there is a fault. It could have been alleviated using this code when their is a fault:


..
if ($this->client->fault) {
    $this->return_data["error"] .= print_r($result, true);
    return json_encode($this->return_data);
} else {
..

Because the try/catch does not go into the method unless you are using an external object and you want to “catch” its exceptions. But you do not throw an exception in a try/catch block like you did. You use a try/catch block when you implement an operation (object/method), you try the operation if it fails you catch the exception. You don’t do it from within the operation.


try {
  $o = new Object();
  $o->ParticipantSelectByUser();

  # Should never have "throw" in a try/catch block.
} catch ( Exception $e ) {
  // handle the exception...
}