1

Having read the article on this site here, I wrote this following code :

<?php

try{
  annundefinedmethod();
}
catch(RuntimeException $e){
  echo 'Runtime exception called';
} 
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
catch(Exception $e){
  echo 'General exception called';
}

?>

I wanted to show an error based on the proper exception for the function calling in the try block. However, all above exceptions didn't work, I still got an error saying uncaught error : call to undefined method.... what has gone wrong with my code?

januaryananda
  • 397
  • 1
  • 5
  • 15
  • Possible duplicate of http://stackoverflow.com/questions/7116995/is-it-possible-in-php-to-prevent-fatal-error-call-to-undefined-function – bloodstix Jan 15 '16 at 15:12

3 Answers3

1

You can't catch fatal errors in PHP. You may use 'is_callable' or 'function_exists' for this situation.

You may throw your own catch if you like:

try{
    if (!is_callable('annundefinedmethod')) {
        throw new BadFunctionCallException();
    } 
}
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
sanderbee
  • 694
  • 7
  • 24
  • but, the code inside the try block does create an error, doesn't it? So why the error couldn't be automatically caught by the exception without having to throw it or check it using an if-else statement first? – januaryananda Jan 15 '16 at 15:46
  • Because it's a fatal error. Thay can be catched with a custom error handler. See this post for example http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error – sanderbee Jan 15 '16 at 20:04
0

In PHP 5.x, you have to explicitly test if the function is callable first. You can't catch this type of error.

In PHP 7, errors such as this one are actually Error objects that implement Throwable. Error is a the new PHP 7 new base class for thrown internal PHP errors.

In this particular case, you are actually getting an Error object that implements Throwable. If you add a catch on either one of those types, you will be able to catch this error.

try {
    annundefinedmethod();
}
catch (Error $e) {
    //$e->getMessage() == "Call to undefined function annundefinedmethod()"
}
jbafford
  • 5,528
  • 1
  • 24
  • 37
0

Quite simply an undefined method isn't the code throwing an exception.

try{
  throw new Exception;
}
catch(RuntimeException $e){
  echo 'Runtime exception called';
} 
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
catch(Exception $e){
  echo 'General exception called';
}

You can also pass and call messages through in your exceptions and write them out in the catch block:

try{
  throw new Exception('some useful error message');
}
catch(RuntimeException $e){
  echo 'Runtime exception called';
} 
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
catch(Exception $e){
  echo $e->getMessage();
}

This is the same for the other type of exceptions you mention:

try{
  throw new RuntimeException;
}
catch(RuntimeException $e){
  echo 'Runtime exception called';
} 
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
catch(Exception $e){
  echo 'General exception called';
}

And

try{
  throw new BadFunctionCallException;
}
catch(RuntimeException $e){
  echo 'Runtime exception called';
} 
catch(BadFunctionCallException $e){
  echo 'Bad function call exception called';
}
catch(Exception $e){
  echo 'General exception called';
}
Garry Welding
  • 3,599
  • 1
  • 29
  • 46
  • but, the code inside the try block does create an error, doesn't it? So why the error couldn't be automatically caught by the exception without having to throw it first? – januaryananda Jan 15 '16 at 15:45
  • quite confused about the term exception. So could you provide me the code that throws exception automatically without throwing it? – januaryananda Jan 15 '16 at 15:47
  • On the site I provided above, in the section "Architecture & Best-Practices" on the second paragraph. – januaryananda Jan 15 '16 at 15:49