Page 1 of 1

Try and catch

Posted: Fri Apr 07, 2006 10:39 am
by someberry
I come from a JAVA background, and I know that I dont necessarily use this feature in JAVA correctly (I generally will not use the throw), and it seems to have come to my demise.

In JAVA, something such as this would work fine, which is a shame it wont work in PHP :(

Code: Select all

try
{
     $new_array = $old_array_1 + $old_array_2;
}
catch(Exception $e)
{
     echo('Oh dear, and error seemed to have been caused: ' . $e);
}
I read the PHP manual, but it seemed to insist on using throws, as I don't really use in JAVA. Any ideas?

Thanks,
someberry

Posted: Fri Apr 07, 2006 10:51 am
by feyd
What do you think isn't working? PHP itself doesn't throw anything.

Posted: Fri Apr 07, 2006 10:54 am
by someberry
With JAVA, if there is an error in the try, then the catch will pick it up, and then you can do anything you want with it, as long as the catch statement uses the exception, NumberFormatException etc.

However, the catch doesn't seem to be picking up my error, which results in a fatal error.

Thanks.

Posted: Fri Apr 07, 2006 10:57 am
by Christopher
There is good information here about exceptions with examples.

Re: Try and catch

Posted: Fri Apr 07, 2006 10:59 am
by someberry
someberry wrote:I read the PHP manual, but it seemed to insist on using throws, as I don't really use in JAVA. Any ideas?
:)

Posted: Fri Apr 07, 2006 11:03 am
by feyd
as I said, PHP itself does not throw anything. It fires warnings, errors and such. There are no plans to change that behaviour anytime soon. You could use set_error_handler().

Re: Try and catch

Posted: Fri Apr 07, 2006 11:40 am
by Christopher
someberry wrote:With JAVA, if there is an error in the try, then the catch will pick it up, and then you can do anything you want with it, as long as the catch statement uses the exception, NumberFormatException etc.
I think you are misunderstanding. In Java, if something throws an error within a catch block it will be caught. However, because many operations throw an exception in Java, you are not seeing an explicit throw, but it is there. In PHP you must be explicit.

someberry wrote:I read the PHP manual, but it seemed to insist on using throws, as I don't really use in JAVA. Any ideas?
Sounds like you have two choices, use throws or use Java.

Posted: Fri Apr 07, 2006 12:51 pm
by Ollie Saunders
as I said, PHP itself does not throw anything. It fires warnings, errors and such. There are no plans to change that behaviour anytime soon. You could use set_error_handler().
Yeah bit like this:

Example One

Code: Select all

function myHandler($num,$str,$file,$line,$context) {
   // append stuff from $file and $line into $str if you wish
   throw new Exception($str,$num);
}
set_error_handler('myHandler');

$foo = array();
try {
   $len = strlen($foo['undefined_index']);
} catch(Exception $e) {
   // stuff
}
This would cause all not fatal errors to throw errors. But its a bit of an indirect way of doing it. This way you are letting PHP trigger_errors then throwing them and catching when you could just use a bit of logic to check for an error and throw directly, with your own more meaningful messages....like this:

Example Two

Code: Select all

$foo = array();
try {
   if(!isset($foo['undefined_index'])) throw new Exception('whatever message');
   $len = strlen($foo['undefined_index']); // to reach here 'undefined_index' would be defined
} catch(Exception $e) {
   // stuff
}
That said you are likely doing lots of operations which will cause PHP to trigger lots of errors then "example one" may save you doing all those checks. But hang on you would have to check through all the errors in the catch statement anyway. So do it as "example two"