In an earlier question , I got the advice to use a try-catch statement on an odbc_connect call. Well, said and done, that's what I've tried to do.
The following code, which tries to connect to a database using bogus login information, does not work as excpected.
<?php
try
{
odbc_connect('BogusDatabase','BogusUser','BogusPassword');
}
catch (Exception $e)
{
echo "Something went wrong!";
}
?>
I would expect the output to be a string saying "Something went wrong!". Instead I get this:
I'm using Wampserver to run my PHP code. I don't know if this is a part of the problem.
As @Aurimas said, you have to use error_handler to do that. http://php.net/manual/en/function.set-error-handler.php
this function look after the errors that occured in your script and call a function that you provide each time an error is throwed.
A simple handler is
function($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
It throws a ErrorException each time an error is raised in your script. so this whole script will behave as you expect :
set_error_handler(function($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
});
try
{
odbc_connect('BogusDatabase','BogusUser','BogusPassword');
}
catch (Exception $e)
{
echo "Something went wrong!";
}
On PHP manual Exeptions :
Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException
Take a look at the ErrorException and set your custom error handle, to catch these errors.
The function is not throwing an Exception. Instead, it's triggering a warning. If you don't want warnings to display, you can use the error_reporting() to display only actual errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.