-2

When i initiate a class which does not exist , It throws the error, I Don't want to halted by that error . So i try trycatch method , But it still giving me same error, Can someone explain why this error is not been catched I tried

 try{$obj = new classname();}
 catch(Exception $e){ echo 'class does not exist, move on' ;}

Fatal error: Class 'classname' not found in C:\WampDeveloper\Websites\localhost\webroot\index.php on line 4

Can someone explain why this error can not be catched ?

Is their is another way to catch and handle this kind of errors ?

UPDATE

We can catch mysql fatal errors by try catch method , So don't say fatal errors can not be handeled by try catch method

johnDoe
  • 63
  • 2
  • 8
  • 4
    Because it's not an exception, it's a fatal error. – AbraCadaver Nov 02 '16 at 18:32
  • http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – AbraCadaver Nov 02 '16 at 18:33
  • Exceptions are generated by running code which encounters an error. Your code isn't able to run, so a try/catch doesn't do anything. You'd correct this by not trying to instantiate a class which doesn't exist. – David Nov 02 '16 at 18:33
  • Regarding your update... MySQL errors and PHP errors are very, very different things. PHP *can* catch runtime errors generated by failing database queries, because the PHP code is itself running without error. – David Nov 02 '16 at 18:35
  • @AbraCadaver Can u explain why database Fatal errors can be handle by try catch method., – johnDoe Nov 02 '16 at 18:37
  • Because `catch` catches exceptions not errors. Check the link I posted. Also: _We can catch mysql fatal errors by try catch method_ Really? Show me? – AbraCadaver Nov 02 '16 at 18:39
  • @user7025447: When integrating with the database, the PHP code is simply communicating with an external system. That external system can fail, but the PHP code itself hasn't failed. It can successfully detect that failure and handle it accordingly. However, if you write PHP code which itself is invalid and can't execute, then it can't self-detect its own failure. Code has to be able to execute in order to handle exceptions. – David Nov 02 '16 at 18:39
  • @AbraCadaver In PHP 7, catch catches throwables, including the error class ;) – Xorifelse Nov 02 '16 at 18:41
  • @Xorifelse: Good to know! What are the chances OP is using PHP 7? – AbraCadaver Nov 02 '16 at 18:42
  • @David Thanks david and all other members for replying. I don't really understand whats going on but got the idea :D – johnDoe Nov 02 '16 at 18:42
  • @AbraCadaver Read your comment wrong, read changes instead of chances, but.. apparently he has PHP 7. – Xorifelse Nov 02 '16 at 19:32

1 Answers1

0

Two ways to solve this, use a autoloader that runs a custom written function for each object that does not exist so you can try to "include" a file on demand.

function autoload($objname){
  if(is_readable(($f = '/path/to/class/'.$objname.'.php'))){
    include $f;
  } else {
    throw Exception("$f does not exist");
  }
}

spl_autoload_register('autoload');

new classname(); // try to load /path/to/class/classname.php

Or you can upgrade to PHP 7 where the error logic has had little overhaul:

Hierarchy

  • Throwable
    • Error
      • ArithmeticError
      • DivisionByZeroError
      • AssertionError
      • ParseError
      • TypeError
    • Exception

So a code like this would work:

try{
  $obj = new classname();
} catch(Error $er){
  echo 'class does not exist, move on';
} catch(Exception $ex){
  echo 'a custom exception has been thrown:' . $ex->getMessage();
} catch(Throwable $t){
  // Obsolete code, as Throwables are either Error or Exception, that were caught above.
}
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • it is not working i tried,`try{ $obj = new classname(); } catch(Error $er){ echo 'class does not exist, move on';` But still the same error – johnDoe Nov 02 '16 at 19:11
  • Then perhaps, you already have a working autoloader somewhere, generating that exception? Instead change `Error` to `Throwable` as it will catch that custom exception as well. – Xorifelse Nov 02 '16 at 19:13