11

I am wanting to handle exceptions in my PHP application myself.

When I throw an exception I am wanting to pass along a title to be used in the error page.

Can someone please link me to a good tutorial, or write a clear explanation of how the exception handling actually works (eg how to know what sort of exception you are dealing with, etc.

3 Answers 3

31

Official docs is a good place to start - http://php.net/manual/en/language.exceptions.php.

If it is just a message that you want to capture you would do it at follows;

try{
    throw new Exception("This is your error message");
}catch(Exception $e){
    print $e->getMessage();
}

If you want to capture specific errors you would use:

try{
    throw new SQLException("SQL error message");
}catch(SQLException $e){
    print "SQL Error: ".$e->getMessage();
}catch(Exception $e){
    print "Error: ".$e->getMessage();
}

For the record - you'd need to define SQLException. This can be done as simply as:

class SQLException extends Exception{

}

For a title and message you would extend the Exception class:

class CustomException extends Exception{

    protected $title;

    public function __construct($title, $message, $code = 0, Exception $previous = null) {

        $this->title = $title;

        parent::__construct($message, $code, $previous);

    }

    public function getTitle(){
        return $this->title;
    }

}

You could invoke this using :

try{
    throw new CustomException("My Title", "My error message");
}catch(CustomException $e){
    print $e->getTitle()."<br />".$e->getMessage();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, This answer is awesome. With this example in mind, if I wanted to pass along a title as well as just the message with the exception how would I do that?
W3Schools is not owned by W3C, and is actually a poor resource, see w3fools.com for info
Sorry, I missed off the 'schools' part. I did however think that their guide on exception handling was a decent enough introduction but I'll edit and change to the official docs, for consistency.
@Haliwood - I've updated to answer your question. Hope this helps.
@Matt Better to define a common message and a title inside the customException class __construct as $message = '...' . so it will support for the re usability .
|
3

First, I'd recommend to take a look at corresponding PHP manual page, it's great place to start. Also, you can take a look at Extending Exceptions page - there are some more info about standard exception class, and example of custom exception implementation.

If the question is, how to do some specific action if exception of particular type was thrown, then you just have to specify exception type in catch statement:

    try {
        //do some actions, which may throw exception
    } catch (MyException $e) {
        // Specific exception - do something with it
        // (access specific fields, if necessary)
    } catch (Exception $e) {
        // General exception - log exception details
        // and show user some general error message
    }

Comments

2

Try this as the first thing on your php page(s).

It captures php errors and exceptions.

function php_error($input, $msg = '', $file = '', $line = '', $context = '') {
    if (error_reporting() == 0) return;

    if (is_object($input)) {
        echo "<strong>PHP EXCEPTION: </strong>";
        h_print($input);
        $title  = 'PHP Exception';
        $error  = 'Exception';
        $code   = null;
    } else {
        if ($input == E_STRICT) return;
        if ($input != E_ERROR) return;
        $title  = 'PHP Error';
        $error  = $msg.' in <strong>'.$file.'</strong> on <strong>line '.$line.'</strong>.';
        $code   = null;
    }

    debug($title, $error, $code);

}

set_error_handler('php_error');
set_exception_handler('php_error');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.