1

I'm trying to make a catch try this:

Config::set( 'database.connections.information_schema', array ( 
    'driver'     =>  'mysql', 
    'host'       =>  'localhost', 
    'port'       =>  '3306', 
    'database'   =>  'information_schema', 
    'username'   =>  'root', 
    'password'   =>  '1', 
    'charset'    =>  'utf8', 
    'collation'  =>  'utf8_unicode_ci', 
    'prefix'     =>  ''
    // 'strict'    => false, 
)); 

try 
{
    $datos = DB::connection('information_schema')->getDatabaseName();    
    echo "Conectado correctamente a la base de datos: ".$datos.".";
} 
catch (Exception $e) 
{
    echo 'Error';
}

And when he goes right ejectua the try but obviously when to go for the catch does not show echo

Santiago Muñoz
  • 169
  • 1
  • 2
  • 12
  • What is exactly your problem? You think it never enter the catch when an error happen? – olibiaz Mar 02 '16 at 21:32
  • Here is a good example on how to use try catch block for php http://stackoverflow.com/questions/17549584/how-to-efficiently-use-try-catch-blocks-in-php – Can Celik Mar 02 '16 at 21:43

2 Answers2

4

Change your

catch (Exception $e) 

to

catch (\Exception $e) 

More info on importing/aliasing namespaces; you can also add use Exception; in the top of the file. Find more in the docs

Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37
-1

I suppose the database connection is in some function/method. If it is not, just put the code in some function.

In case of error, we jump out of the function with return command and continue only if the code inside of try block succeeded.

Something like this:

...
try {
    $datos = DB::connection('information_schema')->getDatabaseName();    
} catch (Exception $e) {
    echo 'Error';
    return false;
}
//connected successfully
echo "Conectado correctamente a la base de datos: ".$datos.".";
...
Miro
  • 1,879
  • 14
  • 12