0

I'm playing around with custom PHP exceptions for the first time, and would like some help with cleaning up some code. In particular, I'm catching PDO errors, and have written a class to mail myself a stack trace of the errors. The way that I'm currently doing things is as follows:

try {

//db stuff

} catch (PDOException $e) {

    throw new My_Own_Exception($e);
        exit;

}   

where my My_Own_Exception does the job with:

class My_Own_Exception extends Exception
{

  /*code to mail myself error info: this part works!
}

While the above works, I feel like I should be able to just write something cleaner such as:

try {

    } catch (My_Own_Exception $e) {
        exit;
    }

where My_Own_Exception is an extension of the PDOException class. A few questions about this: First, is the second way the better approach? Second, if so, is it possible? Third, if it is possible, how do I let PHP know that My_Own_Exception "exists" if My_Own_Exception is never instantiated anywhere? Hopefully the third question makes some sense: my gut tells me that if I can make that happen, then my approach should be possible.

Eric
  • 1,209
  • 1
  • 17
  • 34
  • 3
    Are you sure an exception is the correct place for logic such as emailing yourself stuff? Why not just catch the PDOExceptions and mail them? You cannot influence which exceptions PHP is going to throw, so catch them as in the first code and deal with them - don't "convert" one into another for trivial matters – kero May 30 '14 at 17:30
  • 1
    codereview.stackexchange.com – Félix Adriyel Gagnon-Grenier May 30 '14 at 17:38
  • 1
    I think what you are looking for is [**a custom exception handler**](http://www.php.net/manual/en/function.set-exception-handler.php). Basically, what this means is that .. not you will have a flexible, simple way handling exceptions better than what you had hopped for. – samayo May 30 '14 at 17:39
  • I agree with @kingkero I don't think you want to use another `Exception` for this. – cmorrissey May 30 '14 at 17:49
  • Thanks @kingkero. As you can probably tell, I'm new to the idea of creating custom exceptions, and I'm not sure what "best practices" are; though based on your comment, it's clear that I don't even know what "good practices" are just yet! Also, based on your thoughts, I'm not sure if this question will have an "answer" that makes good sense. If you could move his comment to a proposed answer, then I'd be happy to accept it. – Eric May 30 '14 at 22:58

2 Answers2

1

I don't think that an exception is the correct place for logic, it should contain information about the error. A PDOException is useful because you know it originates from your PDO code, if you throw a MyException instead, you need to at least give more (useful) information.

This being set, you should read BiVOC's comment on your original question.

If you have a custom exception handler, you can then differentiate via instanceof.

function exception_handler($exception) {
    if ($exception instanceof PDOException) {
        //mail
    }
    //always log/etc.
}
Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51
0

What you are trying to do wont work, because nothing in the PDO code will throw your exception, unfortunately. So you are going to have to throw it yourself, like you were in the first example.

Also, the exit in the first example will never be hit.

Ascherer
  • 8,223
  • 3
  • 42
  • 60