Try and catch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Try and catch

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What do you think isn't working? PHP itself doesn't throw anything.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

There is good information here about exceptions with examples.
(#10850)
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Try and catch

Post 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?
:)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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().
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Try and catch

Post 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.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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"
Post Reply