Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Below I have written an example that shows you how to use exceptions in general (non specific to your situation) and further down something that is more specific to you (still using exceptions). These first two examples will handle 1 error at a time. The third example I have provided gives an example of how to handle multiple errors and exceptions.</p> <p>Most of the explanation is in the comments of the code, so be sure to look through it thoroughly :)</p> <p><em><strong>General Exception Handling</em></strong></p> <pre><code>&lt;?php //Define some variables to work with $var = false; $var2 = false; try { //Outer try echo 'Do something here!&lt;br /&gt;'; try { //Inner try if($var !== true) { //Fail throw new Exception('$var is not true',123); //Exception is thrown (caught 2 lines down) } } catch (Exception $e) { //Exception caught here echo 'InnerError# '.$e-&gt;getCode().': '.$e-&gt;getMessage().'&lt;br /&gt;'; //Exception handled (in this case printed to screen) } //Code is continuing here even after the exception was thrown echo 'Do something else here!&lt;br /&gt;'; if($var2 !== true) { //Fail throw new Exception('$var2 is not true', 456); //Exception is thrown (caught 6 lines down) } //Code fails to run as the exception above has been thrown and jumps straight into the below catch echo 'Do the third thing here!&lt;br /&gt;'; } catch (Exception $e) { //Exception caught here echo 'Error # '.$e-&gt;getCode().': '.$e-&gt;getMessage().' on line '.$e-&gt;getLine().' in '.$e-&gt;getFile().'&lt;br /&gt;'; //Exception handled (in this case printed to screen) } //Code is continuting here even after both of the exceptions echo 'Do even more stuff here!&lt;br /&gt;'; ?&gt; </code></pre> <ul> <li><a href="http://php.net/manual/en/language.exceptions.php" rel="nofollow">http://php.net/manual/en/language.exceptions.php</a></li> <li><a href="http://www.php.net/manual/en/class.exception.php" rel="nofollow">http://www.php.net/manual/en/class.exception.php</a></li> </ul> <p>Standard Exception class constructor:</p> <pre><code>public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] ) </code></pre> <hr> <p><em><strong>Custom Exceptions</em></strong></p> <p>Now, relating this to your example, you could do something along these lines:</p> <pre><code>&lt;?php class customException extends Exception { //Create a custom exception handler that allows you to pass more arguments in the constructor public function __construct($errorString, $errorNumber, $errorFile, $errorLine) { $this-&gt;message = $errorString; //Using the Exception class to store our information $this-&gt;code = $errorNumber; $this-&gt;file = $errorFile; $this-&gt;line = $errorLine; } } function err2Exception($errNo, $errStr, $errFile, $errLine) { //This function converts the error into an exception throw new customException($errStr, $errNo, $errFile, $errLine); //Throw the customException } set_error_handler('err2Exception'); //Set the error handler to the above function try { assert(1==2); //This fails, calls the function err2Exception with the correct arguments, throws the error and is caught below } catch (Exception $e) { //Error caught as an Exception here //Echo out the details (or log them, or whatever you want to do with them) echo 'Error String: '.$e-&gt;getMessage().'&lt;br /&gt;'; echo 'Error Number: '.$e-&gt;getCode().'&lt;br /&gt;'; echo 'File containing error: '.$e-&gt;getFile().'&lt;br /&gt;'; echo 'Line with error: '.$e-&gt;getLine().'&lt;br /&gt;'; } ?&gt; </code></pre> <p><a href="http://php.net/manual/en/function.set-error-handler.php" rel="nofollow">http://php.net/manual/en/function.set-error-handler.php</a></p> <p>Output of above code:</p> <blockquote> <p>Error String: assert(): Assertion failed</p> <p>Error Number: 2</p> <p>File containing error: 18</p> <p>Line with error: /var/www/test2.php</p> </blockquote> <p>You can apply the concepts of nesting <code>try</code>/<code>catch</code> statements in the first code example with this second example of custom error handling.</p> <p><em><strong>Handling Multiple Errors/Exceptions</em></strong></p> <pre><code>&lt;?php class errorLogger { //create an errorLogger class private $errors; //Stores all errors public function addError($errCode, $errMsg, $errFile = null, $errLine = null) { //Manually add an error $this-&gt;errors[] = array( //Add to the error list 'code' =&gt; $errCode, 'message' =&gt; $errMsg, 'file' =&gt; $errFile, 'line' =&gt; $errLine ); } public function addException($exception) { //Add an exception to the error list $this-&gt;errors[] = array( //Add to the error list 'code' =&gt; $exception-&gt;getCode(), 'message' =&gt; $exception-&gt;getMessage(), 'file' =&gt; $exception-&gt;getFile(), 'line' =&gt; $exception-&gt;getLine() ); } public function getErrors() { //Return all of the errors return $this-&gt;errors; } public function numErrors() { //Return the number of errors return count($this-&gt;errors); } } $el = new errorLogger(); //New errorLogger set_error_handler(array($el, 'addError')); //Set the default error handler as our errorLoggers addError method set_exception_handler(array($el, 'addException')); //Set the default exception handler as our errorLoggers addException method if(!is_numeric('a')) //Will fail $el-&gt;addError('Invalid number', 1); //Adds a new error if(($name = 'Dave') !== 'Fred') //Will fail $el-&gt;addError('Invalid name ('.$name.')', 2, 'test.php', 40); //Adds another error assert(1==2); //Something random that fails (non fatal) also adds to the errorLogger try { if('Cats' !== 'Dogs') //Will fail throw new Exception('Cats are not Dogs', 14); //Throws an exception } catch (Exception $ex) { //Exception caught $el-&gt;addException($ex); //Adds exception to the errorLogger } trigger_error('Big bad wolf blew the house down!'); //Manually trigger an error //throw new Exception('Random exception', 123); //Throw an exception that isn't caught by any try/catch statement //(this is also added to the errorLogger, but any code under this is not run if it is uncommented as it isn't in a try/catch block) //Prints out some echo '&lt;pre&gt;'.PHP_EOL; echo 'There are '.$el-&gt;numErrors().' errors:'.PHP_EOL; //Get the number of errors print_r($el-&gt;getErrors()); echo '&lt;/pre&gt;'.PHP_EOL; ?&gt; </code></pre> <p>Obviously you can change and adapt the <code>errorLogger</code> class to specifically suit your needs.</p> <p>Output of above code:</p> <blockquote> <p>There are 5 errors: </p> <p>Array (</p> <pre><code>[0] =&gt; Array ( [code] =&gt; Invalid number [message] =&gt; 1 [file] =&gt; [line] =&gt; ) [1] =&gt; Array ( [code] =&gt; Invalid name (Dave) [message] =&gt; 2 [file] =&gt; test.php [line] =&gt; 10 ) [2] =&gt; Array ( [code] =&gt; 2 [message] =&gt; assert(): Assertion failed [file] =&gt; /var/www/test.php [line] =&gt; 42 ) [3] =&gt; Array ( [code] =&gt; 14 [message] =&gt; Cats are not Dogs [file] =&gt; /var/www/test.php [line] =&gt; 46 ) [4] =&gt; Array ( [code] =&gt; 1024 [message] =&gt; Big bad wolf blew the house down! [file] =&gt; /var/www/test.php [line] =&gt; 51 ) </code></pre> <p>)</p> </blockquote> <p>The above code allows you to:</p> <ul> <li>Throw exceptions and add them to the <code>errorLogger</code></li> <li>Handle any unhandled situations from random functions that would normally cause errors to be displayed</li> <li>Add your own errors manually</li> <li>Trigger errors ( <a href="http://uk3.php.net/trigger_error" rel="nofollow">http://uk3.php.net/trigger_error</a> )</li> </ul> <p>You can then display/log/whatever all of the errors at a later point.</p> <p><em>NB: All of the above code can be copied and pasted directly to give you something to experiment with</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload