# Cakephp 3.x: try catch is not working

**URL:** <https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602>\
**Category:** Need Help\
**Created:** [June 22, 2016, 3:09pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602 "2016-06-22T15:09:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusuf\_cse](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yusuf_cse/32/270_2.png) [@yusuf\_cse](https://discourse.cakephp.org/u/yusuf_cse)\
**Post date:** [June 22, 2016, 3:09pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/1 "2016-06-22T15:09:00Z")

</div>

Hi all  
I want to catch error or exception or PDO exception using php try,catch block in cakephp 3 controller. But this is not working.

Can any one help me that how can i use try and catch block in a controller.

Thanks

---

<div class="post-metadata">

**Author:** ![jeremyharris](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jeremyharris/32/66_2.png) [@jeremyharris](https://discourse.cakephp.org/u/jeremyharris)\
**Post date:** [June 22, 2016, 6:58pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/2 "2016-06-22T18:58:11Z")

</div>

You would use it the same way you would in any other PHP code. Are you sure that an exception is being raised? Can you show some code?

---

<div class="post-metadata">

**Author:** ![yusuf\_cse](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yusuf_cse/32/270_2.png) [@yusuf\_cse](https://discourse.cakephp.org/u/yusuf_cse)\
**Post date:** [June 23, 2016, 2:30pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/3 "2016-06-23T14:30:42Z")

</div>

Hi Jere

I would like to give you many many thanks. Yes i used the following code which raised a PDO exception obviously as i get the error from error log saying that “Unknown artist\_order column”

try {  
$data = $this-\>glbl();  
$get\_group\_artists = $data[‘group\_artists’]-\>find(‘all’)  
-\>where([‘group\_id’ =\> $id])  
-\>order([‘artist\_order’ =\> ‘ASC’]);  
} catch (PDOException $e) {  
$this-\>log(“Error Occured”, ‘error’);  
}  
Above code doesn’t write any anything in the error log file. Please assist me how to solve the problem.

---

<div class="post-metadata">

**Author:** ![jeremyharris](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jeremyharris/32/66_2.png) [@jeremyharris](https://discourse.cakephp.org/u/jeremyharris)\
**Post date:** [June 23, 2016, 5:08pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/4 "2016-06-23T17:08:19Z")

</div>

Well, time to do some debugging then!

The first thing I would do is throw a debug statement in there, just to be extra sure:

```php
try {
  $data = $this->glbl();
  $get_group_artists = $data['group_artists']->find('all')
    ->where(['group_id' => $id])
    ->order(['artist_order' => 'ASC']);
} catch (PDOException $e) {
  // add this to view the exception details
  debug($e);
  $this->log("Error Occured", 'error');
}

```

Next, you can check your log setup to make sure your application is configured to log:

```php
// add this somewhere before that code to check out your log setup
foreach (\Cake\Log\Log::configured() as $config) {
    debug(\Cake\Log\Log::config($config));
}

```

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [June 24, 2016, 2:37am UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/5 "2016-06-24T02:37:21Z")

</div>

In the [book](http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#debugging-queries-and-resultsets) it says that the query itself doesn’t not retrieve the results. You must execute with `->all()` or `->first()`

---

<div class="post-metadata">

**Author:** ![yusuf\_cse](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/yusuf_cse/32/270_2.png) [@yusuf\_cse](https://discourse.cakephp.org/u/yusuf_cse)\
**Post date:** [June 24, 2016, 9:45am UTC](https://discourse.cakephp.org/t/cakephp-3-x-try-catch-is-not-working/602/6 "2016-06-24T09:45:53Z")

</div>

I’ve run given code in two different ways as you suggested. In addition i have also run my code. All outputs are given in chronological order in the link

[image link](https://mayusuf.wordpress.com/2016/06/24/try-catch-error-codes-output/)

1. foreach (\Cake\Log\Log::configured() as $config) {  
debug(\Cake\Log\Log::config($config));  
}

2. 

```
 foreach (\Cake\Log\Log::configured() as $config) {
     debug(\Cake\Log\Log::config($config));
 }

```

exit();

```
    try {
        $data = $this->glbl();
        $get_group_artists = $data['group_artists']->find('all')
                ->where(['group_id' => $id])
                ->order(['artist_order' => 'ASC']);
    } catch (PDOException $e) {
        // add this to view the exception details
        debug($e);
        $this->log("Error Occured", 'error');
    }

```

1. 

$data = $this-\>glbl();  
$get\_group\_artists = $data[‘group\_artists’]-\>find(‘all’)  
-\>where([‘group\_id’ =\> $id])  
-\>order([‘artist\_order’ =\> ‘ASC’]);
