title: How to override the Exception Class and not to display the Fatal error
body: <p>I am trying to write a class that communicate with an API. I would like to override the standard Exception class in PHP to return the message, error code that I want.</p>
  
  <p>I have added this extension</p>
  
  <pre><code>&lt;?php namespace API;
  
  /**
   * API Exception
   *
   * @package ICWS
   */
  class ApiException extends \Exception
  {
      public function __construct($message, $code = 0)
      {
          // Custom ICWS API Exception Message
          $apiMessage = 'ICWS API Error: ' . $message;
  
          //More code to come to custom the error message/code.....
  
          // Run parent construct using custom message
          parent::__construct($apiMessage, $code);
      }
  }
  ?&gt; 
  </code></pre>
  
  <p>Then when needed I create new ApiException like so</p>
  
  <pre><code>    throw new ApiException($errorMessage, $errorNo);
  </code></pre>
  
  <p>Finally I wrap the function that throws an exception by  <code>try{} catch()</code> block to capture the exception.</p>
  
  <p>However, I still get the a <code>fatal error</code> instead of just the message that I provided.</p>
  
  <p>Here is my code </p>
  
  <pre><code>public function createSession($userID, $password){
  
      $data = array('userID' =&gt; $userID,
                    'password' =&gt; $password);
  
      try {
  
          $data = $this-&gt;_makeCall('POST', 'connection', $data);
  
          $this-&gt;_csrfToken = $data['csrfToken'];
          $this-&gt;_sessionId = $data['sessionId'];
          $this-&gt;_alternateHostList = $data['alternateHostList'];
  
      } catch (Exception $e){
          $this-&gt;_displayError($e);
      }
  }
  
  private function _makeCall($uri, $data = false, $header = array())
  {
      $ch = curl_init();
      $url = $this-&gt;_baseURL . $uri;
  
      //disable the use of cached connection
      curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  
      curl_setopt($ch, CURLOPT_URL, $url);
  
      //return the respond from the API
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
      if(!empty($header)){
          curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
      }
  
      curl_setopt($ch, CURLOPT_POST, true);
  
      if ($data){
          $JSON = json_encode( $data );
          curl_setopt( $ch, CURLOPT_POSTFIELDS, $JSON );
      }
  
  
      $result = curl_exec($ch);
  
      //throw cURL exception
      if($result === false){
          $errorNo = curl_errno($ch);
          $errorMessage = curl_error($ch);
  
          throw new ApiException($errorMessage, $errorNo);
      }   
  
      $result = json_decode($result, true);
  
      //throw API exception
      if(  $this-&gt;_hasAPIError($result) )
      ){          
          throw new ApiException($result['message'], 0);
      }
  
      return $result;
  }
  
  private function _displayError(Exception $e){
      echo 'Error Number: ' . $e-&gt;getCode() . "\n";
      echo 'Error Description: ' . $e-&gt;getMessage() . "\n\n";     
  }
  
  private function _hasAPIError($result){
      if(    isset($result['errorId']) &amp;&amp; !empty($result['errorId'])
          &amp;&amp; isset($result['errorCode']) &amp;&amp; !empty($result['errorCode'])
          &amp;&amp; isset($result['message']) &amp;&amp; !empty($result['message'])
      ){          
          return true;
      }
  
      return false;       
  }
  </code></pre>
  
  <p>I would like to see something like this at then end "if there is an error"</p>
  
  <pre><code>Error Number: 0
  Error Description: ICWS API Error: The authentication process failed
  </code></pre>
  
  <p>This is what I currently get</p>
  
  <pre><code>Fatal error: Uncaught exception 'API\ApiException' with message 'ICWS API Error: The authentication process failed.' in C:\phpsites\icws\API\ICWS.php:130 Stack trace: #0 C:\phpsites\icws\API\ICWS.php(57): API\ICWS-&gt;_makeCall('connection', Array) #1 C:\phpsites\icws\index.php(17): API\ICWS-&gt;createSession('user', 'pass') #2 {main} thrown in C:\phpsites\icws\API\ICWS.php on line 130
  </code></pre>