0

I'm new to try and catch in PHP. When I tried it I expected all the warnings and errors in the $e variable. They are logged in $e but also output on the screen.

My current solution for it is to add @ before $db to prevent the warnings from popping up on the screen. Is there a better solution? Mine feels a little hacky.

<?php
function db() {
    $db = array();
    try {
        @$db = new Database(array(
            'type'     => 'mysql',
            'host'     => 'localhosts',
            'database' => 'megastore',
            'user'     => 'root',
            'password' => ''
        ));
    } catch (Exception $e) {
        echo $e;
    }
    return $db;
}
Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
  • 7
    Exceptions and errors aren't the same thing: try/catch will catch Exceptions, not errors, unles you've explicitly set up an error handler to convert errors to exceptions – Mark Baker Jan 22 '16 at 12:11
  • What database extension are you using? `mysql_` or `mysqli_` or `PDO`? – RiggsFolly Jan 22 '16 at 12:13
  • Error handling has changed in v7: http://php.net/manual/en/class.throwable.php – Alex Blex Jan 22 '16 at 12:15
  • Don't supress errors using `@` – Rajdeep Paul Jan 22 '16 at 12:16
  • 1
    @RajdeepPaul Don't blanket-ban `@`. Example: `$result = @file_put_contents("file.txt","derp"); if( !$result) presentUsefulErrorMessage();`. A proper error handler is definitely a good idea, but sometimes you need localised error handling that just isn't possible without `@`... at least until PHP7. – Niet the Dark Absol Jan 22 '16 at 12:19
  • 2
    Possible duplicate of [PHP try/catch and fatal error](http://stackoverflow.com/questions/12928487/php-try-catch-and-fatal-error) – Niki van Stein Jan 22 '16 at 12:21
  • @RiggsFolly I'm using mysql from a wrapper. https://getkirby.com/docs/toolkit/databases. – Jens Törnell Jan 22 '16 at 12:22
  • @AlexBlex I will use old PHP for some time but thanks for heads up. – Jens Törnell Jan 22 '16 at 12:23
  • @RajdeepPaul. Okay. Can you give alternatives then? – Jens Törnell Jan 22 '16 at 12:24
  • @NiettheDarkAbsol Ah, good point. Noted. :) – Rajdeep Paul Jan 22 '16 at 12:24
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 22 '16 at 12:28
  • The PDO extension comes with Exceptions already Baked in, use that especially as the `mysql_` extension IS DEAD as of PHP7 – RiggsFolly Jan 22 '16 at 12:29

1 Answers1

1

Use the silence operator is not a good idea.
You can use error_reporting() to show only what you want or you can set display_error (using ini_set() or from php.ini) to false so error will not be display on the screen.
EDIT :
This two options is not the same but maybe you can have the same results. For more info read link in php manual.

Isky
  • 1,328
  • 2
  • 14
  • 33
  • Can anyone confirm that this is a better idea than @ and that this is the best idea in this case? – Jens Törnell Jan 22 '16 at 12:25
  • Please dont use the `mysql_` database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Jan 22 '16 at 12:28
  • @RiggsFolly https://github.com/getkirby/kirby/issues/360 "The database class is already built around PDO. The old mysql_ extension is not in use." – Jens Törnell Jan 22 '16 at 13:23