Thursday, 5 February 2015

Exception Handling in PHP, YII

If page not found throw a new exception using

if(($_POST['cons']==='') or ($_POST['cons']===null))
throw new CHttpException(404,'The requested page does not exist.');


The above one Used in Controllers actions If the specified parameters are not specified by user the new exception is thrown

Use the statements in try{}  block and then catch the exceptions at the end of try block

try{
-----------------------
----------------
-------------------------
}catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    Dialog::message('Exception',"Caught exception:$e->getMessage()");
}

Exit the code using die() or exit() functions

These are all identical.  die() is just a straight-up alias to exit(), but even if it isn't, it still acts identically.
When one of these functions is given a string argument, it prints out the string before terminating the process. When it encounters an integer under 255, that integer is considered the return code for the process, which gets passed back to the process which invoked the PHP script. This is particularly useful when writing command line applications (PHP isn't web-only!).
As far as the difference between exitexit(), and exit(0), there really is none. There is definitely no difference between the first two because exit is technically a language construct, not a function, so it can be called with or without parentheses, just like echo. Returning a code of 0means "this program ran successfully/without errors", and while I don't know what exactly happens when you don't pass an argument, PHP.net says that an argument-less exit indicates success, so I would bet it returns 0, though again PHP.net doesn't show a default for the argument.

No comments:

Post a Comment