By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

question about exception

P: 51
The following I try to execute function 'my_test()' only if it exists. But anyway if 'my_test()' doesn't exist, it will stop there before 'echo'.

[PHP]try {
} catch (Exception $e) {}

echo 'Hello World';[/PHP]

Oct 14 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,639
you could test the function with function_exists(). if you want to use try-catch you have to throw an exception to make it work.

Oct 14 '08 #2

Expert 5K+
P: 5,058

You can use the set_error_handler function to set your own error handler, which could throw an exception.

But... it will not work for fatal errors, such as the on your non-existing function would cause. Such an error is considered the equivalent of a Java *compile error*, which the code itself would not be able to handle.

Exceptions are meant to handle runtime errors. Fatal errors will always stop your code, despite your error or exception handling. (In theory at least)

You would need to use the function_exists function, like mentioned in the previous post.
Oct 14 '08 #3

P: 51
Thank you so much everyone. Got it to work now by using function_exists().

Oct 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.