2

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:

Damn! I don't want this!

I'm using Wampserver to run my PHP code. I don't know if this is a part of the problem.

3 Answers 3

6

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!";
}
0
0

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.

2
  • Do you have a non-greek (or non-geek) version of this? :) I've been using PHP for a while now, but I can't understand anything about what is said here.
    – Speldosa
    Sep 15, 2012 at 13:24
  • Look at the @artragis answer ;) Sep 15, 2012 at 16:02
0

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.

2
  • Well, no, I don't want the warning to display. However, I also want to know when something went wrong with connecting to the database so that I could try to fix it, hence, I would like to get to the code in the catch block. I'm lookin at Aurimas link above, but it's like greek to me and I don't really understand how I should implement it.
    – Speldosa
    Sep 15, 2012 at 11:46
  • Well, odbc_connect returns false on error, so you could do something like this: if(!odbc_connect(...)) // error, try to handle it else // ok
    – Nim
    Sep 16, 2012 at 11:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.