6

I'd like to create a new exception, called SecurityException. Where should I put the code?

class SecurityException extends CakeException {};

Thanks!

entropid
  • 6,130
  • 5
  • 32
  • 45
  • Related: http://stackoverflow.com/questions/13590614/throwing-a-custom-503-exception-in-cakephp-2 – trante Nov 30 '12 at 07:50

2 Answers2

8

Create an exceptions.php file, put it on the Lib folder and fill it up with all your *Exception classes. Then include it on your application's bootstrap file.

require APP . 'Lib' . DS . 'exceptions.php';

All exceptions will become available application wide.

luchomolina
  • 1,202
  • 2
  • 13
  • 24
  • 17
    [Here](https://groups.google.com/d/msg/cake-php/tK66NqCYMYw/6-FMj9XyvmYJ)'s another approach: "put exceptions in `([plugin-if-any])/Lib/Error/ Exception/NameOfTheException.php` and use `App::uses('NameOfTheException', 'Error/Exception')` where they're needed. Seemed like a Cake'ish way to do it, and they're not included unless one is actually thrown." – luchomolina Oct 12 '11 at 18:15
  • 1
    @luchomolina it should be an answer too. This is that awkward case when comment gathers more +ses, than the post :) – Serge S. Jul 30 '15 at 07:50
1

I followed luchomolina's 2nd answer (commented on his own answer), and thought it deserved to be an official answer:

Here's another approach: "put exceptions in ([plugin-if-any])/Lib/Error/Exception/NameOfTheException.php and use App::uses('NameOfTheException', 'Error/Exception') where they're needed. Seemed like a Cake'ish way to do it, and they're not included unless one is actually thrown." –luchomolina

Mike T
  • 1,286
  • 11
  • 13