Chttpexception Is Not Setting Statuscode

Hi - I’m having a strange problem. I am throwing below CHTTPException:

throw new CHttpException(404,'Not found');

And catching it in my function. It is being catched correctly and message is also being printed correctly, but the issue is with StatusCode. Seems like CHTTPException is not setting the status code of exception and that’s why instead of 404 I’m getting 500 status code. Below is the catch statement:

catch(Exception $e) {



In above: $e->getCode() is giving me 0 (zero). While if a access $e->statusCode it gives me 404. Why getCode() is giving me zero on exception.

Please suggest.


I think, I got it. There is another parameter in CHTTPException for code. If i throw exception in below way than it works fine:

throw new CHttpException(404,'Not found',404);

But, Why I have to give code two times?

Please suggest.


The getCode() method returns the exception code, not the HTTP status code, you should be using:


Your first version is the correct one to use for throwing the exception.

Hi Keith - Thanks for your reply. I got it. But what exactly is the difference between HTTP Exception Code and HTTP Status Code. I have googled it and found it same. So why there is a facility of two different parameters in YII?


Ravi Verma

No, the exception code is for PHP (see here). It’s a property of the base Exception class and is nothing to do with HTTP status codes.

Yii has extended this class and added the statusCode property, which represents the HTTP status code.