php CodeIgniter 和抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6217786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:40:44  来源:igfitidea点击:

CodeIgniter and throwing exceptions

phpexceptioncodeignitererror-handling

提问by cabaret

I recently handed in a project for school which I built in CodeIgniter. I had to present it to my teacher and when asked how I handled certain errors, he told me to throw exceptions to intercept things a lot earlier in the chain of events.

我最近提交了一个我在 CodeIgniter 中构建的学校项目。我不得不把它呈现给我的老师,当被问及我如何处理某些错误时,他告诉我抛出异常以在事件链的早期拦截事物。

I have learnt how to throw exceptions and how to use try...catchblocks to uh, catch and handle them but somehow, when I started using CodeIgniter, I forgot all about them and didn't really use exceptions anymore.

我已经学会了如何抛出异常以及如何使用try...catch块来捕捉和处理它们,但不知何故,当我开始使用 CodeIgniter 时,我忘记了它们,不再真正使用异常了。

Instead, I just handled my errors 'manually', for lack of a better word: I'd use TRUEand FALSEboolean values to check if, for example, a query executed properly, and I would use the returned boolean to handle the result of the query. If TRUE, I'd go ahead and do my stuff, if FALSEI'd 'manually' throw an error message. The project was very AJAX-dependent and the error messages would pop up in quite a fancy way, dropping down from the top of the page; not sure if this is possible when I throw an exception with throw new Exception? I know that this basically stops the code from executing when the exception is thrown, so wouldn't that break things somehow?

相反,我只是处理我的错误“手动”,由于缺乏一个更好的词:我会使用TRUEFALSE布尔值检查,例如,查询正确执行,我会使用返回的布尔处理的结果询问。如果TRUE,我会继续做我的事情,如果FALSE我“手动”抛出错误消息。该项目非常依赖 AJAX,错误消息会以一种非常奇特的方式弹出,从页面顶部下拉;不确定当我抛出异常时这是否可能throw new Exception?我知道这基本上会在抛出异常时停止执行代码,所以这不会以某种方式破坏吗?

I also seem to remember reading somewhere that throwing exceptions isn't the best practice ever but I can't find the source of this anymore and I'm not quite sure if this is the case; after all, we did learn how to use them in class and I like to believe we learn best practices there, haha.

我似乎还记得在某个地方读到过抛出异常并不是最好的做法,但我再也找不到它的来源,我不太确定是否是这种情况;毕竟,我们确实在课堂上学习了如何使用它们,我相信我们在那里学习了最佳实践,哈哈。

If necessary, I could go back and try to find the piece of code where he pointed out that I should've thrown an exception. However, for now, I'm just wondering whether or not I should use exceptions in my code or handle things manually. What are the best practices regarding this?

如有必要,我可以返回并尝试找到他指出我应该抛出异常的代码段。但是,就目前而言,我只是想知道是否应该在代码中使用异常或手动处理。这方面的最佳做法是什么?

Thanks.

谢谢。

回答by Alejandro García Iglesias

Just FYI, I don't use exceptions in CodeIgniter tho I'm using them a lot in Kohana, just because the framework throws them and everything is designed to work with exceptions unlike CodeIgniter. Using exceptions is a good practice providing all your classes/framework are designed to work with them.

仅供参考,我没有在 CodeIgniter 中使用异常,但我在 Kohana 中经常使用它们,只是因为框架会抛出它们,并且一切都旨在处理与 CodeIgniter 不同的异常。使用异常是一种很好的做法,前提是您的所有类/框架都旨在与它们一起使用。

I don't (really, DON'T) want to enter in framework comparison discussions, but I need to compare two pieces of code to clarify your question, one piece from CI2 and another from Kohana 3 (it born as a branch of CI with better object oriented implementation).

我不想(真的,不想)参与框架比较讨论,但我需要比较两段代码来澄清你的问题,一段来自 CI2,另一段来自 Kohana 3(它是作为 CI 的一个分支诞生的)具有更好的面向对象实现)。

You'll see this CI2 code...

你会看到这个 CI2 代码...

try
{
    $result = $this->db->insert('entries', $this->input->post());

    // This is not useful.
    if ( ! $result)
    {
        throw new Exception();
    }
}
catch (Exception $e)
{
    // Do something
}

It's not very useful. Compare with this Kohana 3 code:

它不是很有用。与此 Kohana 3 代码进行比较:

try
{
    $entry = ORM::factory('blog');
    $entry->values(Request::current()->post());
    $entry->save();
}
catch (ORM_Validation_Exception $e)
{
    Session::instance()->set('form_errors', $e->errors(TRUE));
}

You'll see this is useful, you don't throw the exception, it's thrown by the class that handles the record saving and $e->errorshas all the validation errors. When everything is designed to work with exceptions, you can be sure it's a good practice and a very convenient one. But it's not the case of CI2, so maybe I should say go ahead without using exceptions.

您会发现这很有用,您不会抛出异常,它是由处理记录保存并$e->errors具有所有验证错误的类抛出的。当一切都被设计为处理异常时,您可以确定这是一种很好的做法,也是一种非常方便的做法。但这不是 CI2 的情况,所以也许我应该说继续而不使用异常。



A possible approach to exceptions in CI...

一种可能的 CI 异常处理方法......

try
{
    $this->load->model('blog');
    $this->blog->save_entry($this->input->post());   // Handle validation inside the model with the Form_validation library
}
catch (Validation_Exception $e)   // You throwed your custom exception with the failed validation information
{
    // Do something with your custom exception like the kohana example
    $this->session->set('form_errors', $e->errors());
}

I hope everything is understandable and maybe there's someone with another interesting opinion and a more efficient implementation. Bye.

我希望一切都是可以理解的,也许有人有另一个有趣的意见和更有效的实施。再见。