Click to See Complete Forum and Search --> : Valid use of Try/Catch?


dougal85
01-19-2007, 07:49 PM
OK, Is this a valid use of try{}catch{}


public function __get($var){

try{

return $this->${$var};

} catch (Exception $e){

// handle error how i want

}

}


This could otherwise be done by...


public function __get($var){

if(isset($this->${$var}) && !empty($var)){

return $this->${$var};

} else {

// handle error how i want

}

}



What do you think? I personal think its probably not a good way to do it. However, I thought I'd throw it out there for your views.

NogDog
01-20-2007, 08:17 AM
I think it would be more like:

public function __get($var){
try{
if(!isset($this->$var) {
throw new Exception("Invalid attribute $var");
}
return $this->${$var};
}
catch (Exception $e) {
echo "Oops! ".$e->getMessage();
}
}

dougal85
01-20-2007, 08:20 AM
I see. But how could that possbly ever trigger the exception? so i wondered if there was a point?

Should try/catch be used on all methods?

*edit*

Sorry, ignore this i just re-red your example. It makes sense now.

I'm half asleep. :o

but my other question still stands, should this structure be used on all methods?

NogDog
01-20-2007, 08:27 AM
Here's a working version with some typos and logic errors fixed:

<?php

class Test
{
var $myVar = 1;

public function __get($var){
try{
if(!isset($this->$var)) {
throw new Exception("Invalid attribute '$var'");
}
return $this->$var;
}
catch (Exception $e) {
echo "Oops! ".$e->getMessage();
}
}
}

// try it:
header('Content-Type: text/plain');
$test = new Test();
echo $test->__get('myVar')."\n"; // 1
echo $test->__get('foobar'); // error message
?>

dougal85
01-20-2007, 08:44 AM
Cool, that was very helpful. I understand it now. Just had a coffee too so i'm more awake!

What confuses me is in what situation should this be used?
Once in all methods?
As many times as needed in a method?
I assume it should always be used atleast once.

ahundiak
01-21-2007, 12:23 AM
I would suggest that your method merely throw the exception:

class Test
{
var $myVar = 1;

public function __get($var){
if(!isset($this->$var)) {
throw new Exception("Invalid attribute '$var'");
}
return $this->$var;
}
}

Then it's up to your calling code to catch any generated exceptions.

try {
$test = new Test();
$var1 = $test->var1;
$var2 = $test->var2;
/* Process your data secure in the knowledge that if any data is missing an exception will be thrown */
}
catch (Exception $e)
{
/* Do whatever */
}

dougal85
01-21-2007, 08:34 AM
So, the idea being. General Exceptions are handled in one general place?

Sounds good :)

ahundiak
01-21-2007, 10:03 AM
So, the idea being. General Exceptions are handled in one general place?

Sounds good :)
At least that's how I tend to use them. But Exceptions are one of those thingees with many different approaches. Plenty of threads on them.

Key thing about your example is that, in general, it makes little sense for a method to catch and process it's own exceptions.

Weedpacket
01-21-2007, 11:26 AM
Another thing to keep in mind is that exceptions are pretty expensive things to be throwing around. Conditions that you can anticipate and test for are generally better handled by testing for them deliberately, and leave using exceptions for exceptional cases that actually cause the method to break down.

On the other hand, throwing an exception is often convenient because without them you have to have some other way to indicate an error condition (the way so many of PHP's functions return false on failure, even though it might return 0 on success, suggests why this can be a good thing) and you have to deal with that error condition in every piece of code that uses the function's result (whereas exceptions can just keep bubbling up through the call stack until it is caught).

A routine that opens a file, appends to it, and closes it again would have a try{} block around the file-handling lines, because there are several (more than three) ways in which the code can fail, some of which are outside your control (e.g., even if you check to see that the file exists before opening it, someone else might delete the file during the instant between the check and the actual opening). On top of that, doing all the filesystem checks would end up being slower than just making the attempt and throwing the exception in event of failure.

dougal85
01-21-2007, 11:46 AM
Thanks for the breakdown weedpacket. That was really helpful.

What I would really love to see, would be a small application. That's basic purpose is to demonstrate best practices and techniques, so of course open source. It would answer so many questions for me.

Although, i do realise best practice can often be subject to personal preference, however... there must be away to develop the most popular one.

There isn't something like this out there? is there?