# Catch query exceptions in controller

**URL:** <https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569>\
**Category:** Need Help\
**Created:** [November 24, 2017, 2:00pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569 "2017-11-24T14:00:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ride\_of\_rohirrim](https://avatars.discourse-cdn.com/v4/letter/r/dfb087/32.png) [@ride\_of\_rohirrim](https://discourse.cakephp.org/u/ride_of_rohirrim)\
**Post date:** [November 24, 2017, 2:00pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/1 "2017-11-24T14:00:28Z")

</div>

Hello,

My problem is as follows. I need to catch the exception on query in controller. Query depends on user input, so It is possible(and probable) that many times user will generate error. I want to handle it in my own way.

I tried this:

[https://pastebin.com/tE58S8RH](https://pastebin.com/tE58S8RH)

I searched whole internet, but nothing helped. Tried \PDOException etc., and still nothing. CakePHP 3.4.3 (PHP 5)

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 24, 2017, 3:37pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/2 "2017-11-24T15:37:11Z")

</div>

Your code seems to be valid for me.  
Do you get some error message when the exception should be certainly thrown?

---

<div class="post-metadata">

**Author:** ![ride\_of\_rohirrim](https://avatars.discourse-cdn.com/v4/letter/r/dfb087/32.png) [@ride\_of\_rohirrim](https://discourse.cakephp.org/u/ride_of_rohirrim)\
**Post date:** [November 24, 2017, 7:26pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/3 "2017-11-24T19:26:51Z")

</div>

I get 500 error page. In development mode:

“Database Error  
PDOException”

It’s like Cake handles this exception before me.

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 25, 2017, 9:41am UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/4 "2017-11-25T09:41:32Z")

</div>

Without seeing the full stack trace I would gess that you have a problem connection to the database, that is why you get this error message as connection faliure happens before your query, so your query is not reached at all, so it can not catch the exception.

---

<div class="post-metadata">

**Author:** ![ride\_of\_rohirrim](https://avatars.discourse-cdn.com/v4/letter/r/dfb087/32.png) [@ride\_of\_rohirrim](https://discourse.cakephp.org/u/ride_of_rohirrim)\
**Post date:** [November 25, 2017, 11:40am UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/5 "2017-11-25T11:40:17Z")

</div>

Connection seems to be good - it works fine everywhere else. If data from the the $str is correct, there is no error too. This happens only when query is invalid. Maybe attached will provide some more info.

![s1](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/ee39bc970cc3b99748a96aac7bb3a02be88491d1.png)

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 25, 2017, 12:08pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/6 "2017-11-25T12:08:23Z")

</div>

What is in your `Template/Stars/search.ctp` line 51? There you need a try catch block

---

<div class="post-metadata">

**Author:** ![ride\_of\_rohirrim](https://avatars.discourse-cdn.com/v4/letter/r/dfb087/32.png) [@ride\_of\_rohirrim](https://discourse.cakephp.org/u/ride_of_rohirrim)\
**Post date:** [November 25, 2017, 12:23pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/7 "2017-11-25T12:23:00Z")

</div>

> [@rrd](#):
>
> search.ctp

Its: \<?php if( isset($q) && $q-\>count() \> 0 ): ?\>

Good point. Seems like the problem is that $q is lazy loaded and exception happens in Template, not Controller. How can I force the query to happen in Controller? (I guess Template is no good place for try/catch).

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [November 25, 2017, 6:36pm UTC](https://discourse.cakephp.org/t/catch-query-exceptions-in-controller/3569/8 "2017-11-25T18:36:08Z")

</div>

Call $q-\>all() in the controller before you use set()
