Mongodb 连接失败,怎么避免抛错影响正常流程

发布网友 发布时间:2022-04-23 13:09

我来回答

1个回答

热心网友 时间:2022-04-08 02:24

PHP操作MongoDB的 exception handling没有什么特别的地方,和其他的场景一样,使用try catch来处理。

PHP MongDB client包含了一些exeception的类:

MongoDB\Driver\Exception\AuthenticationException
MongoDB\Driver\Exception\BulkWriteException
MongoDB\Driver\Exception\ConnectionException
MongoDB\Driver\Exception\ConnectionTimeoutException
MongoDB\Driver\Exception\Exception
MongoDB\Driver\Exception\ExecutionTimeoutException
MongoDB\Driver\Exception\InvalidArgumentException
MongoDB\Driver\Exception\LogicException
MongoDB\Driver\Exception\RuntimeException
MongoDB\Driver\Exception\SSLConnectionException
MongoDB\Driver\Exception\UnexpectedValueException
MongoDB\Driver\Exception\WriteException
但是您估计为了简化,只会统一处理MongoDB\Exception\Exception。

正如一楼的朋友所言,可以做一些类来封装此类操作使得事情简洁一些。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com