• The "@" symbol tells PHP to suppress all errors caused by the contents of our code behind it.
<?php
Try
{
throw new Exception (' An error has occurred! ', 42);
}
catch (Exception $e)
{
echo ' number '. $e->getcode (). ': '. $e->getmessage ();
}
?>
The exception exception class is provided in PHP 5, which provides some built-in methods in addition to constructors:
GetCode (): Returns the code passed to the constructor getmessage (): Returns the message passed to the constructor GetFile (): Returns the full path of the code file that generated the exception getline (): Returns the line number of the code that generated the exception in the code file · Gettrace (): Returns an array containing the code fallback path that generated the exception gettraceasstring (): Returns the same information as the Gettrace () direction, which is formatted as a string. _tostring (): Allows a simple display of a exception object and gives the information that all of the above methods can provide.
<?php
Class MyException extends exception
{
function _tostring ()
{
Return ' <table><tr><td>exception '. $this->getcode ().
': '. $this->getmessage (). ' <br/>in '. $this->getfile ().
' On line '. $this->getline (). ' </td></tr></table> ';
}
}
Try
{
throw new MyException (' Error! ', 42);
}
catch (MyException $e)
{
Echo $e; The _tostring () method is called automatically.
}
?>
PHP exception handling mechanism