There are a few functions in php that throw errors when they fail, such as ftp_login
. If I have this code.
try {
$result = ftp_login($conn_id, $ftp_user_name, 'incorrectPassword');
if (!$result) {
throw new Exception('Could not login.');
}
} catch (Exception $e) {
echo $e->getMessage() . ' - ' . error_get_last();
}
It will not catch because of the error. Is the appropriate thing in this case to use an Error Control Operator like so?
try {
$result = @ftp_login($conn_id, $ftp_user_name, 'incorrectPassword');
if (!$result) {
throw new Exception('Could not login.');
}
} catch (Exception $e) {
echo $e->getMessage() . ' - ' . error_get_last();
}
This allows me to react to the error, however it suppresses errors and would be a major performance hit. How can I judge which is the right approach, and is there any other alternatives I'm not considering?
1 Answer 1
Register your own error handler with set_error_handler
. Within that error handler, throw the exception.
If now ftp_open
throws an error, the exception from your custom error handler behave just as if ftp_open
threw it.
-
I know this is not a debugging site, but how could I apply set_error_handler to this function? I'm having trouble applying the information in the docs to this situation. php.net/manual/en/function.set-error-handler.phpGoose– Goose2016年09月21日 16:54:07 +00:00Commented Sep 21, 2016 at 16:54
-
1A error handler is called when a php error occurs; it overrides the default behaviour (which is printing the error out)marstato– marstato2016年09月21日 17:18:08 +00:00Commented Sep 21, 2016 at 17:18
Explore related questions
See similar questions with these tags.