1

I have written a very simple code to catch an exception in PHP but it still shows an error on the page . I am not able to understand why it does not catch the exception .

<?php

try
{
    session_start();

    echo    ($counter);
}
catch(Exception $e)
{
    echo "caught exception";
}

?>

1 Answers1

2

Before php 7 you could only catch exceptions, not errors, but since php 7 you have a new interface called \Throwable that is more general than just exceptions and also the Error class was introduced that implements Throwable

http://php.net/manual/en/class.throwable.php

There are currently two types of Throwable objects, that is Exceptions and Errors, So now you can also catch Errors, However Fatal errors still break your code

you can try

<?php

try
{
    session_start();

    echo    ($counter);
}
catch(\Exception $e)
{
    echo "caught exception";
}
catch(\Error $e)
{
    echo "caught error";
}

or you car try

try
{
    session_start();

    echo    ($counter);
}
catch(\Throwable $e)
{
    echo "caught exception";
}
Nathanael
  • 870
  • 5
  • 11