...

View Full Version : Custom Exception checking....



loki421
01-10-2012, 09:22 PM
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:


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:


// 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? :p

Looking forward to any input you guys and girls might have :)

Thanks!

Fou-Lu
01-10-2012, 09:26 PM
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.

loki421
01-10-2012, 10:00 PM
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 :p

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?

Fou-Lu
01-10-2012, 10:04 PM
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).

loki421
01-10-2012, 10:26 PM
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;



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! :)

Fou-Lu
01-11-2012, 01:22 AM
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.

Dormilich
01-11-2012, 06:48 AM
I see, so I could run a check at the bottom of the page for instance for something like;



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

// 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";
}
// example for throwing exception
function connect()
{
$conn = new MySQLi(HOST, USER, PASS, TABLE);
if ($conn->connect_error)
{
throw new RuntimeException($conn->connect_error);
}
return $conn;
}

loki421
01-11-2012, 09:39 AM
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! :D

loki421
01-11-2012, 09:42 AM
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

// 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";
}
// example for throwing exception
function connect()
{
$conn = new MySQLi(HOST, USER, PASS, TABLE);
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 :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum