Intellij:通过IDE创建一个随机数,用作错误号/错误代码?

时间:2017-06-02 01:46:41

标签: intellij-idea

见这里:The proper way to do exception handling

说这段代码:

function changeBookAuthor(int $id, string $newName){
  if(!$newName){
   throw new MyAppException('No author name was provided');
  }

  $book = Books::find($id);

  if(!$book){
   throw new MyAppException('The provided book id could not be found');
  }
//..
}

我想将其更改为:

 function changeBookAuthor(int $id, string $newName){
      if(!$newName){
       throw new MyAppException('No author name was provided', <SOMEVERYRANDOMNUMBER>);
      }

      $book = Books::find($id);

      if(!$book){
       throw new MyAppException('The provided book id could not be found', <SOMEVERYRANDOMNUMBER>);
      }
    //..
    }

可以帮助我选择随机数吗?

1 个答案:

答案 0 :(得分:0)

我个人使用不同类型的Exception而不是异常代码。

例如:

try{
   ...
} catch (PDOException e1){
   // Show a message that we could not do SQL work
} catch (NumberFormatException e2){
   // Show a message that input was not a valid number
} catch (Exception e){
   // I'm not sure what was wrong but definitely there was some thing wrong
}

但如果您仍然需要随机数,请转到https://www.random.org,有一些数字生成器,复制值并将它们定义为代码中的常量(我猜您使用的是PHP)