# \[Solved\] Custom Exception

**URL:** <https://discourse.cakephp.org/t/solved-custom-exception/4683>\
**Category:** Uncategorized\
**Created:** [August 8, 2018, 1:39pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683 "2018-08-08T13:39:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![carbonera](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/carbonera/32/1114_2.png) [@carbonera](https://discourse.cakephp.org/u/carbonera)\
**Post date:** [August 8, 2018, 1:39pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/1 "2018-08-08T13:39:00Z")

</div>

Hi Foks  
I’m facing the follow problem:

I don’t want use the cake’s exception/error pages, I just want throw an Exception message, catch it and show to my end user.

For example:

```
function my_function() {
   throw \Exception('My message');
} 

try {
   $this->my_function();
} catch (\Exception $e) {
   echo $e->getMessages();
}

```

But I don’t know how to only get the message like I’d do with raw PHP.

---

<div class="post-metadata">

**Author:** ![Speudyland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/speudyland/32/569_2.png) [@Speudyland](https://discourse.cakephp.org/u/Speudyland)\
**Post date:** [August 8, 2018, 1:53pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/2 "2018-08-08T13:53:36Z")

</div>

You can create your [custom exception class](https://book.cakephp.org/3.0/en/development/errors.html#creating-your-own-application-exceptions) 😉

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [August 8, 2018, 2:21pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/3 "2018-08-08T14:21:45Z")

</div>

@carbonera Cake’s detailed error pages are only shown when debug in on. In production when you turn off debug all exceptions are converted to 404 or 500 errors. So you just need to customize the two template files in `src/Template/Errors`.  
[https://github.com/cakephp/app/tree/master/src/Template/Error](https://github.com/cakephp/app/tree/master/src/Template/Error)

---

<div class="post-metadata">

**Author:** ![carbonera](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/carbonera/32/1114_2.png) [@carbonera](https://discourse.cakephp.org/u/carbonera)\
**Post date:** [August 8, 2018, 4:54pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/4 "2018-08-08T16:54:06Z")

</div>

There is no way to treat exceptions like we usually do when we are programming in raw PHP ?

---

<div class="post-metadata">

**Author:** ![carbonera](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/carbonera/32/1114_2.png) [@carbonera](https://discourse.cakephp.org/u/carbonera)\
**Post date:** [August 8, 2018, 4:55pm UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/5 "2018-08-08T16:55:11Z")

</div>

I already readed this section, but for me it’s a bit confused. I tried to put the examples inside cake’s structure, but it not worked.

---

<div class="post-metadata">

**Author:** ![carbonera](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/carbonera/32/1114_2.png) [@carbonera](https://discourse.cakephp.org/u/carbonera)\
**Post date:** [August 9, 2018, 11:58am UTC](https://discourse.cakephp.org/t/solved-custom-exception/4683/6 "2018-08-09T11:58:10Z")

</div>

I found the “solution”  
I put an back slash before the Exception, so, this way I could use the php’s Exception instead of the cake’s Exception. I know that in my example I have put the back slash, but I haven’t do it in all places.

Thanks to all.
