CodeIgniter和抛出异常 [英] CodeIgniter and throwing exceptions
问题描述
我最近递交了一个我在CodeIgniter建的学校项目。我不得不把它提交给我的老师,当我问我如何处理某些错误时,他告诉我在事件链中早些时候拦截异常。
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.
我学会了如何抛出异常,以及如何使用 try ... catch
阻塞到uh,捕捉和处理它们,但不知何故,当我开始使用CodeIgniter,我忘了所有他们并没有真的使用例外了。
I have learnt how to throw exceptions and how to use try...catch
blocks 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.
相反,我只是手动处理我的错误,因为缺少一个更好的词:我会使用 TRUE
和 FALSE
布尔值来检查例如查询是否正确执行,我将使用返回的布尔值来处理查询结果。如果 TRUE
,我会继续做我的东西,如果 FALSE
我会'手动'信息。该项目非常依赖于AJAX,并且错误消息将以相当奇怪的方式弹出,从页面的顶部下降;不确定这是否可能,当我抛出一个异常 throw new Exception
?我知道这基本上停止代码执行时抛出异常,所以不会破坏的东西在某种程度上?
Instead, I just handled my errors 'manually', for lack of a better word: I'd use TRUE
and FALSE
boolean 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 FALSE
I'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?
我也似乎记得读某处抛出异常不是最好的做法,但我找不到这个来源,我不太确定如果这是情况;毕竟,我们没有学习如何在课堂上使用它们,我喜欢相信我们在那里学习最佳实践。哈哈。
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?
感谢。
推荐答案
只是FYI,我不使用CodeIgniter中的异常,我在Kohana中使用他们,只是因为框架抛出它们,一切都设计为处理异常与CodeIgniter不同。使用异常是一个好的做法,只要你的所有类/框架都设计为与他们一起工作。
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.
我不想(真的,不要)想进入框架比较讨论,但我需要比较两个代码来澄清你的问题,一个从CI2和另一个从Kohana 3(它出生作为一个分支的CI与更好的面向对象的实现)。
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代码...
You'll see this CI2 code...
try
{
$result = $this->db->insert('entries', $this->input->post());
// This is not useful.
if ( ! $result)
{
throw new Exception();
}
}
catch (Exception $e)
{
// Do something
}
这不是很有用。与这个Kohana 3代码比较:
It's not very useful. Compare with this Kohana 3 code:
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));
}
你会看到这很有用,它由处理记录保存的类抛出,并且 $ e->错误
具有所有验证错误。当一切都设计为处理异常时,你可以确定它是一个好的做法和一个非常方便的。但这不是CI2的情况,所以也许我应该说,没有使用异常。
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->errors
has 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.
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.
这篇关于CodeIgniter和抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!