[Solved] Custom Exception

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.

You can create your custom exception class :wink:

1 Like

@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

1 Like

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

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.

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.