草庐IT

try-Catch

全部标签

PHP - try/catch 的开销是否高于 if/then?

在处理一些遗留代码时,我遇到了大量的Try/Catch语句。Try/Catch不是他们在我的Zend认证类(class)中教授的东西,而且10年来我没有与使用它的其他PHP开发人员一起工作。与执行if语句相比,Try/Catch是否有额外的开销?是什么让它比其他选择更受欢迎或更不受欢迎? 最佳答案 我不认为它们彼此真正相关。If语句用于确定分支逻辑。Try/Catch是处理发生的错误。可以在Catchblock中处理会使程序停止的异常。 关于PHP-try/catch的开销是否高于if/

php - try catch laravel 不适用于重复输入

我在LaravelController中使用以下代码。并得到username的重复错误,但我需要通过try-catch来处理它。此代码无效。insert(['username'=>$request->username,'phone'=>$request->phone,'status'=>1]);}catch(Exception$exception){$errormsg='Databaseerror!'.$exception->getCode();}returnResponse::json(['success'=>$result,'errormsg'=>$errormsg]);}}我遇到

php - 如何捕获 require_once/include_once 异常?

我正在尝试编写类似这样的代码:getMessage();echo"Aerrorocurredwhentryingtoinclude'$include'.Errormessage:$message";}}我已经尝试了require_once和include_once,但是trycatch没有捕捉到异常。我怎样才能捕捉到这个fatalerror/警告异常? 最佳答案 由于include/require不会抛出异常,因此请先检查您要包含的文件是否存在且可读。例如:$inc='path/to/my/include/file.php';if(

php - catch 不工作

我很困惑。是什么导致“catch”无法正常工作?我该如何解决?getMessage());}?>实际输出[27-Apr-201009:43:24]PHPFatalerror:Uncaughtexception'Exception'withmessage'BOOM'in/mycode/exception_problem/index.php:4Stacktrace:#0{main}thrownin/mycode/exception_problem/index.phponline4期望的输出shouldhappen:BOOMPHP版本5.2.3在php_info()中,我没有看到任何可以禁用

php - 捕获接口(interface)异常。

我对PHP中的异常处理有疑问。我有很多异常(exception),意思是一样的:找不到东西。所有这些异常都实现了接口(interface)(不是类)NotFoundException。所以我的问题是:可以检查异常是否在catchblock中实现了接口(interface)。我知道我可以将NotFoundException接口(interface)更改为一个类,但一些异常已经扩展到另一个异常。(示例:CategoryNotFoundException扩展CategoryException并实现NotFoundException)。为什么我需要这个接口(interface)?当一个页面正在

php - Symfony2 : error trying to send an email with Swiftmailer

当我尝试使用swift发送电子邮件时遇到以下问题Symfony2项目:刷新电子邮件队列时发生异常:预期响应代码220但得到代码“”,消息为“。我在localhost中,我尝试使用我的主机OVH的邮件。这是我的config.yml:#SwiftmailerConfigurationswiftmailer:transport:"%mailer_transport%"auth_mode:"%mailer_auth_mode%"host:"%mailer_host%"port:"%mailer_port%"username:"%mailer_user%"password:"%mailer_pa

php - 如何处理其他 try catch block 中的异常?

我的例子:classCustomExceptionextends\Exception{}classFirstClass{functionmethod(){try{$get=external();if(!isset($get['ok'])){thrownewCustomException;}return$get;}catch(Exception$ex){echo'ERROR1';die();}}}classSecondClass{functionget(){try{$firstClass=newFirstClass();$get=$firstClass->method();}catch(

php - symfony 验证路由是否被授予/评估返回 redirectToRoute 语句

我有一个symfony2Controller,我想在其中执行以下操作:try{return$this->redirect($request->headers->get('referer'));}catch(Exception$e){return$this->redirectToRoute('home');}它不起作用。这个想法是考虑到这个Controller中发生的事情,用户的权限可能会改变。所以我想知道他是否还可以访问当前路由,如果可以,则重定向到当前路由,如果不能,则重定向到家。我该怎么做?非常感谢! 最佳答案 我没有找到一个好

用于测试 catch block 的 PHPunit

我有这样一个代码:publicfunctionone(){try{$this->two();}catch(Exception$E){$this->three();}}我如何测试调用了$this->three()函数?我尝试“按代码模拟”$this->two()并抛出错误而不是它的原始代码,但最终错误被phpunit本身捕获。尝试了setExpectedException,但它也没有解决问题-catch再次在phpunit中运行,只是被忽略了。函数$this->three()在这两种情况下都没有被调用。谢谢! 最佳答案 问题是所描述的

php - 在 Silex 中捕获 "Allowed memory size exhausted" fatal error

是否可以使用ErrorHandler/ExceptionHandler模块在Silex中捕获“允许的[n]字节的内存大小耗尽”fatalerror?一个简单的测试用例展示了如何轻松捕获其他类型的fatalerror-例如,以下将捕获PHPStringsizeoverflowfatalerror:useSymfony\Component\Debug\ErrorHandler;useSymfony\Component\Debug\ExceptionHandler;$errorHandler=function($e){error_log("Caughtanerror!");};ErrorHa