简体   繁体   English

我如何找出所有可能由一段PHP代码抛出的错误?

[英]How can I find out all the errors that can possibly be thrown by a block of PHP code?

I want to know what errors can be thrown that I am not catching, for dynamic code - not static code. 我想知道对于动态代码而不是静态代码,可能会引发哪些错误,但我没有捕捉到。 For example, my code may run without throwing any Exceptions for 10 years and then throw UncaughtException 例如,我的代码可以运行10年而不会引发任何异常,然后引发UncaughtException

I want to specifically (non-generically) catch every type of Exception that can be thrown by the methods I am using. 我想专门(非一般性地)捕获我所使用的方法可能抛出的每种类型的Exception。 How can I know what Exceptions MAY be thrown by these methods? 我怎么知道这些方法可能抛出什么异常?

I want to non-generically catch every type of error that can possibly be thrown for a section of PHP code. 我想以非一般性的方式捕获可能会在一段PHP代码中引发的每种类型的错误。

Examples of exceptions that may be thrown: 可能引发的异常示例:

PDOException
ExpiredException

Now I have this around everything: 现在,我在所有事情上都有:

try{
 ...
}catch(Exception $e){
 ...
}

I'd like to replace it with something like this: 我想用这样的东西代替它:

try{
 ...
}catch(PDOException $e){
 ...
}catch(ExpiredException $e){
 ...
}catch(Exception $e){
 ...
}

I'd like to be confident that I am catching all different kinds of Exception that can be thrown by the methods in the section of code 我想确信我正在捕获代码部分中的方法可能引发的所有各种异常

And if I catch all Exceptions individually, will it be safe to remove this part?: 如果我逐个捕获所有异常,可以安全删除这部分吗?:

catch(Exception $e){
 ... 
}

Or are there methods which will simply throw Exception? 还是有一些简单地抛出Exception的方法?

My solution preference list (1 is the most-preferred solution): 我的解决方案首选项列表(1是最优选的解决方案):

1: A flag I can turn on that will cause php.exe to warn me about each and every possible type of Exception that is not specifically being caught 1:我可以打开的标志将导致php.exe警告我每种未明确捕获的异常类型

2: A way to individually check each method and see what errors can be thrown. 2:一种单独检查每种方法并查看会引发哪些错误的方法。 Is the documentation the only way to check? 文档是检查的唯一方法吗? or is there some IDE or PHP block that will tell me which Exceptions may be thrown by individual methods? 还是有一些IDE或PHP块可以告诉我各个方法可能抛出哪些异常?

You can catch all exceptions without even call try . 您甚至无需调用try就可以捕获所有异常。

function hello($e)
{
  if ($e instanceof PDOException){
  echo "something".
}else echo $e->getMessage();


}


set_exception_handler('hello');

this should catch all exceptions. 这应该捕获所有异常。

UPDATE 2 更新2

I've edited the code so you know what exceptions will be thrown using get_class(); 我已经编辑了代码,所以您知道使用get_class();会抛出哪些异常get_class();

class mycustomException extends Exception{} // making new exception
function hello($e)
{
    if ($e instanceof PDOException){ // exception already known
        echo "something";
}else{

        echo get_class($e); // get exception name.
    }


}


set_exception_handler('hello');
throw new mycustomException(); // throw exception that we made.

Well if you are talking exceptions, you already have the answer 好吧,如果您在谈论例外情况,那么您已经有了答案

try{
 ...
}catch(PDOException $e){
 ...
}catch(ExpiredException $e){
 ...
}catch(Exception $e){
   echo get_class($e); // get's the class of unrecorded exceptions.
   //catches any exceptions missed by the above
}

Now if you are talking about "errors" you can do is use a custom error handler 现在,如果您正在谈论“错误”,则可以使用自定义错误处理程序

  if(!function_exists('myErrorHandler')){
      function myErrorHandler($severity, $message, $file = 'UNKNOWN', $line = 'UNKNOWN')
      {

         throw new ErrorException(
             $message,
             1,
             $severity,
             $file,
             $line
         );
      }

      set_error_handler('myErrorHandler');
 }

What it does is convert all PHP errors to exceptions or rather to ErrorExceptoins . 它的作用是将所有PHP错误都转换为异常,或者转换为ErrorExceptoins

Now you can go a step further and use these other two functions. 现在,您可以进一步使用其他两个功能。

register_shutdown_function register_shutdown_function

AND

set_exception_handler set_exception_handler

I'm actually working on porting what I use to it's own stand alone github project . 我实际上正在将自己使用的东西移植到它自己的独立github项目中 Right after I finish my eJinn project, which you may be interested in. eJinn is designed to build exception classes based off a config file, so you can have one error per exception file and unique error codes in a project. 当我完成您可能感兴趣的eJinn项目之后,eJinn旨在基于配置文件构建异常类,因此每个异常文件和一个项目中的唯一错误代码都可能有一个错误。

You can set callback function by using register_shutdown_function() which will call on every end of your php code execution. 您可以使用register_shutdown_function()设置回调函数,该函数将在您的php代码执行的每一端调用。 In this callback function you can check whether any error occurs or not using error_get_last(). 在此回调函数中,您可以使用error_get_last()检查是否发生任何错误。 For Example: 例如:

// Register shutdown function
register_shutdown_function("shutdownTracker");

// Define all error types you want to catch and handle
define('E_FATAL', E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);

function shutdownTracker() {
    $error = error_get_last(); // This will return empty if no error occurs while executing php code.
    if(!empty($error) && ($error['type'] & E_FATAL)) {
        // Write your code here to handle you error

    }
}

Note: You should include this code on top of your code. 注意:您应将此代码放在代码之上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM