简体   繁体   English

在现有代码中捕获Zend_DB异常的最佳方法?

[英]Best way to catch Zend_DB exceptions in already existing code?

I need to catch Zend_DB exceptions at all places in existing code. 我需要在现有代码的所有地方捕获Zend_DB异常。 What would be the best way and place to do it? 最好的方式和地点是什么?

One tedious way I think is to wrap all query code in try catch block but there are so many queries in multiple files. 我认为一种乏味的方法是将所有查询代码包装在try catch块中,但是在多个文件中有很多查询。 So looking for better and alternate option. 因此,寻找更好的替代选择。

Thanks for your help. 谢谢你的帮助。

To me, the key is @bububaba's comment to your question: Catch and do what? 对我而言,关键是@bububaba对您的问题的评论:抓住并做什么?

The easiest thing to do - so you don't have to dig into db/mapper/model/repository/service controller code - is to just modify your ErrorController to "do something different" when the exception is a Zend_Db_Exception . 最简单的事情-不必钻入db / mapper / model / repository / service控制器代码-当异常是Zend_Db_Exception时,只需将ErrorController修改为“做不同的事情”即可。

But that's really top-level stuff, the last gasp chance to catch the priceless Ming vase before it hits the floor and shatters into a million pieces, probably so far away from the thrown error that you are missing out on more meaningful ways of handling the error. 但这确实是顶级的东西,最后一次喘息的机会是在价格不菲的Ming花瓶落到地板上并粉碎成一百万个碎片之前抓住它,这可能与抛出的错误相距太远,以至于您错过了更有意义的处理方法。错误。 In general, the closer you are to the throw, the better is your knowledge of the context in which that throw occurred, which then informs/guides the best way to handle it (ex: let it bubble up to the top, catch/rethrow, catch/eat, etc.). 一般而言,您越接近掷球,就越了解掷球发生的背景,然后可以告知/指导最佳的掷球方法(例如:让其冒泡到顶端,抓住/重新掷球) ,抓/吃等)。 In that sense, there is probably real benefit in wrapping up smaller chunks into try/catch blocks. 从这种意义上讲,将较小的块包装到try / catch块中可能会有真正的好处。

Just thinking out loud. 只是大声思考。 YMMV. YMMV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM