Php try catch not working

try     
{
    $matrix = Query::take("SELECT moo"); //this makes 0 sense

    while($row = mysqli_fetch_array($matrix, MYSQL_BOTH)) //and thus this line should be an error
    {

    }

    return 'something';
}
catch(Exception $e)
{
    return 'nothing';   
}

However instead of just going to catch part and returning nothing it shows a warning Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in the line starting with while. I have never came up to using exceptions in php, but used them a lot in C# and it seems in PHP they are working differently or, as always, I am missing something obvious.

asked Sep 11, 2012 at 20:50

Php try catch not working

4

You can't handle Warnings/Errors with try-catch blocks, because they aren't exceptions. If you want to handle warnings/errors, you have to register your own error handler with set_error_handler.

But it's better to fix this issue, because you could prevent it.

Jeff

13.7k10 gold badges52 silver badges100 bronze badges

answered Sep 11, 2012 at 20:54

Php try catch not working

4

Exception is only subclass of Throwable. To catch error you can try to do one of the following:

try {

    catch (\Exception $e) {
       //do something when exception is thrown
}
catch (\Error $e) {
  //do something when error is thrown
}

OR more inclusive solution

try {

catch (\Exception $e) {
   //do something when exception is thrown
}
catch (\Throwable $e) {
  //do something when Throwable is thrown
}

BTW : Java has similar behaviour.

answered Jan 11, 2018 at 7:49

Gilad TiramGilad Tiram

3993 silver badges3 bronze badges

4

In PHP a warning is not an exception. Generally the best practice would be to use defensive coding to make sure the result is what you expect it to be.

answered Sep 11, 2012 at 20:53

Php try catch not working

datasagedatasage

18.9k2 gold badges47 silver badges53 bronze badges

Welp, unfortunately this is the issue about PHP. Try/catch statements will catch Exceptions, but what you're receiving is an old-school PHP error.

You'll have to catch an error like this with: http://php.net/manual/en/function.set-error-handler.php

Either that or check to see if $matrix is a mysqli_result object prior to performing mysqli_fetch_array.

answered Sep 11, 2012 at 20:55

Php try catch not working

normmcgarrynormmcgarry

6612 gold badges6 silver badges21 bronze badges

1

PHP is generating a warning, not an exception. Warnings can't be caught. They are more like compiler warnings in C#.

answered Sep 11, 2012 at 20:59

Php try catch not working

adhominemadhominem

1,0749 silver badges24 bronze badges

This is something that I’ve run into a lot while programming. I’ll create a try catch statement when attempting to access a resource from a third party API and there are 10 different exceptions that need to be caught in order to prevent a 500 error from being thrown. In some cases I want to catch each individual exception but in others I want to catch all Exceptions with one catch method.

The following PHP code catches all Exceptions in a try catch statement.

try {
   $resource = $client->getResources([
      $resource_id
   ]);
} catch (\Throwable $e) {
   //throw $e;
   $resource = '';
}

catch (\Throwable $e) will catch all exceptions and allow you to handle the Exception rather than adding multiple catch statements targeted at catching specific Exceptions.

Can we use try catch in PHP?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

Can we use try without catch in PHP?

Try, throw and catch Proper exception code should include: try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal.

How can I get 500 error in PHP?

Below are common troubleshooting steps that can be taken to resolve a 500 Internal Server Error:.
Check the error logs..
Check the . htaccess file..
Check your PHP resources..
Check CGI/Perl scripts..

Can PHP try have multiple catches?

PHP allows a series of catch blocks following a try block to handle different exception cases. Various catch blocks may be employed to handle predefined exceptions and errors as well as user defined exceptions.