[Solved] Custom Exception


#1

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.


#2

You can create your custom exception class :wink:


#3

@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


#4

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


#5

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.


#6

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.