Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts

    Custom Exception checking....

    Hi all

    I'm fiddling with custom exception classes and am wondering if I can do something cool.

    Is there a way to check if an exception has been thrown throughout the app?

    For instance, I have a simple customException class as so:
    PHP Code:
    class customException extends Exception
        
    {
            
            public 
    $errorMsg '';
            
            public function 
    errorMessage(){
                
                
    //error message
                
    $this -> errorMsg '<h1>Error!</h1> Exception found on line '$this -> getLine().' in file '.$this -> getFile().' <br /><br />Message: '.$this -> getMessage();
                
                return 
    $this -> errorMsg;
                
            }
            
        } 
    Now I was thinking, if I could check the public $errorMsg of this class after all my server processing is done and redirect / show an error?

    So, here's my thinking:
    PHP Code:
    // something like...
    if(customException -> errorMsg != ""){
        
    // show a js popup or redirect to error page and display customException -> errorMsg?

    But my problem is I don't know what the variable is for customException if it's been thrown. Of course I could instantiate the class at runtime, say $exception_cls = new customException(), but surely if an exception is thrown at some point then that has a seperate instance so my var ($exception_cls -> errorMsg) is empty?

    Hope that makes sense?

    Looking forward to any input you guys and girls might have

    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You mean keep track of the errors within the exception? Its a little bizarre to do that; exceptions are used to trigger a notification to the caller (or stack of caller) that it has encountered a situation it doesn't know how to deal with, and that the caller should deal with it instead. So during the catch you would collect the exceptions into some type of handler here like an array or logger.
    An alternative would be to use a static instead. Create a static array within there, and just keep appending it to self::$msgs;. That can be retrieved directly from customException and as static will contain the same information between each instance of the object.

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    You mean keep track of the errors within the exception? Its a little bizarre to do that; exceptions are used to trigger a notification to the caller (or stack of caller) that it has encountered a situation it doesn't know how to deal with, and that the caller should deal with it instead. So during the catch you would collect the exceptions into some type of handler here like an array or logger.
    An alternative would be to use a static instead. Create a static array within there, and just keep appending it to self::$msgs;. That can be retrieved directly from customException and as static will contain the same information between each instance of the object.
    Thanks for the reply

    Yeah I guess I might just be trying to fiddle a bit too much lol.

    My thinking is this - If for instance I'm running my index.php page, and I'm making a call to my db, and for some reason it fails (wrong password say), my current setup would throw an exception and would print directly at the top of the page (where I've called the db from), so I get an ugly error like: 'Db connection failed etc etc' over the top of my pretty html

    So I was thinking if mybe there is a way that I could display this error a little better, like in a nice js popup or maybe redirect the use to an error page or somehting? Maybe I'm just looking at the wrong way to do things lol. I just thought that instead of printing directly to the page, if I could store it within the class to check at the bottom of the page, but I don't know how to check if an instance of the exception class exists? And if it does, if the errorMsg var is not null.

    What do you reckon?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's just a matter of handling. The try/catch that is in use must just be printing the $e->getMessage() from the error which shows up in place. Instead of printing it, log it to a variable, an array or write a Logger class to take a collection of all the errors that are recoverable and display them neatly at a later location.
    Unless you mean an actual warning or notice, in which case these are not exceptions. You can write an ErrorException handler to deal with them as if they are exceptions, but they will show up in place. You would need to fix the errors to work gracefully instead of triggering an error, or disable your error reporting (both actually should be done).

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's just a matter of handling. The try/catch that is in use must just be printing the $e->getMessage() from the error which shows up in place. Instead of printing it, log it to a variable, an array or write a Logger class to take a collection of all the errors that are recoverable and display them neatly at a later location.
    Unless you mean an actual warning or notice, in which case these are not exceptions. You can write an ErrorException handler to deal with them as if they are exceptions, but they will show up in place. You would need to fix the errors to work gracefully instead of triggering an error, or disable your error reporting (both actually should be done).
    I see, so I could run a check at the bottom of the page for instance for something like;

    PHP Code:
    if($e -> errorMsg != ""){
    // show a popup and print $e -> errorMsg?

    I'm assuming that when an error is caught, it's assigned to a global var of $e (in the original post)?

    So within the try/catch block, the catch would simply remain empty, or maybe push the value into an array like you suggested?

    Thanks for the help dude, most appreciated If you could let me know if $e is global then I think I can take it from here.

    Thanks again!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'd push the value into an array since that way you can keep capturing on $e to save some memory and not worry of overwriting the error.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by loki421 View Post
    I see, so I could run a check at the bottom of the page for instance for something like;

    PHP Code:
    if($e -> errorMsg != ""){
    // show a popup and print $e -> errorMsg?

    I think you did not get (yet) how exceptions work.

    if in one place of your programme an error situation occurs, you throw an instance of the appropriate Exception class. all this code is enclosed in a try ... catch block which will deal with the handling
    PHP Code:
    // pseudo code
    try
    {
        
    // any of these parts could throw an exception in an error situation
        // connect db

        // query db

        // load template

        // fill template with DB data

        // output
    }
    catch (
    Exception $e// this is where you have your exception object
    {
        
    // send data
        
    mail("admin@example.org""error"$e->getMessage(), "From: noreply@example.org");
        
    // handle error
        
    include "error500.html";

    PHP Code:
    // example for throwing exception
    function connect()
    {
        
    $conn = new MySQLi(HOSTUSERPASSTABLE);
        if (
    $conn->connect_error)
        {
            throw new 
    RuntimeException($conn->connect_error);
        }
        return 
    $conn;

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Fou-Lu View Post
    I'd push the value into an array since that way you can keep capturing on $e to save some memory and not worry of overwriting the error.
    Awesome, thanks dude!

  • #9
    Regular Coder
    Join Date
    Feb 2009
    Location
    Worcester
    Posts
    172
    Thanks
    13
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Dormilich View Post
    I think you did not get (yet) how exceptions work.

    if in one place of your programme an error situation occurs, you throw an instance of the appropriate Exception class. all this code is enclosed in a try ... catch block which will deal with the handling
    PHP Code:
    // pseudo code
    try
    {
        
    // any of these parts could throw an exception in an error situation
        // connect db

        // query db

        // load template

        // fill template with DB data

        // output
    }
    catch (
    Exception $e// this is where you have your exception object
    {
        
    // send data
        
    mail("admin@example.org""error"$e->getMessage(), "From: noreply@example.org");
        
    // handle error
        
    include "error500.html";

    PHP Code:
    // example for throwing exception
    function connect()
    {
        
    $conn = new MySQLi(HOSTUSERPASSTABLE);
        if (
    $conn->connect_error)
        {
            throw new 
    RuntimeException($conn->connect_error);
        }
        return 
    $conn;

    Thanks for the info I was just trying to do something funcky with them really, and this has helped to clear up a few things.

    Thanks for your time


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •