Search the Community
Showing results for tags 'exception'.
hello, i am learning PHP, but when i read the next section PHP exception, i do not understand about "class", "public" and why it is used "extends", i hope you can help me solve the problem!!!, Thank you. So i am trying to learn English, if i have misspelled i hope you forgive!!!
Know what you are dealing with : ERROR AND EXCEPTION WHY THIS? When we debug our code that means we find errors and fix them. so it is wise to know about them whom we are dealing with GETTING STARTED:I cant see anything. where to find error?Errors will not always be visible to you. you will see what your php.ini setting is set to show. There is some directive which handle this behaviour.http://in2.php.net/m...en/ini.list.php find the directives here"display_error""error_reporting""error_log" (for error logging)You can set it in php.ini directly (Recomended for development server where you have access to them) or you can set it it using ini_set(). (extra reference)see the principle of changing configuration of php.inihttp://in2.php.net/m...nfiguration.phphttp://in2.php.net/m...anges.modes.php IF YOU USE INI_SET() , MAKE SURE IT IS THE TOP MOST CODE OF YOUR CODE OR INCLUDE/REQUIRE TREEError types: Syntactical error Runtime error Logical error Syntactical error: behaviour:This error means you are not following the grammer of PHP. you had mistaken somewhere that it can not parse your code. as it does not parse your code, this errors show you prior to running the code. so when you see thi error your code is not being run.How to fix them?It is the most easier to fix errors. it happends usually when we make typo or forgot to close braces or put expression terminator (";") etc. though when you see the error you have not to search your code randomly, php will tell you where to look for. the showed error line is not always the origin of the error.it could be prior to that line.READ THIShttp://in3.php.net/m...l/en/tokens.phphere you can see the list of avialable parser tokens. so next time when you will see them it won't be hard for you to fin the reason.prevention is better than cureYou can avoid much of parse error if you write well readable code followinga any code standard. likehttp://pear.php.net/...g-standards.phphttp://framework.zen...g-standard.html and using decent IDE(eg netbeans) or text editor(eg notepad++)http://netbeans.orghttp://notepad-plus-plus.org/Runtime Error:behaviour:Your code is gramatically right. your code has been parsed and it has turned into opcode. now it is time for running it. in runtime there may be such case arises when your program does not do what you expect. like you are trying to get an index of an array which does not exist, etchow to fix them?This is the part we spent mostly to debug our code. There is different level of error Fatal error Warning Notice Fatal error: as it is saying, it is fatal. when such error arised it exits the scripts. YOU MUST FIX IT Warning: It level is lower than Fatal error. but is origin of unexpected behaviour even can lead to fatal error. YOU MUST FIX ITNotice: Low level error. does not harm much. but can lead to higher level error. so YOU SHOULD FIX ITLogical error:behaviour:your code grammer is correct it runs well but it has problem with the logic. It is the hardes part of debugging codes. it is problem with your logic somewhere in the code, which prevent you to outputting expected result.How to fix?PHP cant help you with that. you have to do it yourself. you can do that by whitebox testing or blackbox testing. there is unit test for php which can also help you in that. How would i know so many different error?Runtime errors are documented in every function and method in. you will find that on the manual. when you see any error with particular function just go to manual and see what exception/error it can throw in certain situation. SOMETHING MODERATED:How to handle error?Error handling principle is not same in production and development enviourment. You would not like to show your code faults publicaly. would you? in that case you will log them somewhere instead of showing them.http://in.php.net/ma...n.error-log.phpthere is another option, recruite a error handler which will handle all the error as YOU WANT.http://php.net/set_error_handler I want to throw my own error. how to?there is trigger_error() which will let you throw your own error. remember user level error has also the three types, like Fatal error,warning,notice.check this for available error constant http://in.php.net/ma...c.constants.php TOWARD MORE ADVANCE:EXception?! what is it?Exception is a state where your code is not doing what you expected and you will have chance to terminate the program gracefully. as opposed to Errors which is state, unrecoverable(though there is conceptually difference in exception of php and other language like java. 'notice' and 'warning' is some kind of error but they are recoverable, program does not exist when they occurs)http://in2.php.net/m....exceptions.php Why we will use exception? why php API uses exception (eg PDO)?PHP already documented exception in their manual. there is no need to write down again. the most imporatnt features of exception is, it is CATCHABLE and it can BUBBLE UP. Most of newer API include exception for its robustness. It also needs less code to write. it is readable. People should use exception for error handling instead of old error handling methods (like $error=array()). is there any callback handler same as for error handler?yes there is.http://in.php.net/manual/en/function.set-exception-handler.php. it will also give you freedom to handle exception customacllyWe can catch exception. can we catch error?Yes we can. EVEN WE CAN CATCH THE FATAL ERROR. we can use error handler to catch error and then throw ErrorException. http://in.php.net/ma...orexception.php look at the example. errorException is also exception which extends exception class. they are intenteded for handling error with behaviour of exception. Why would i want to catch and throw errors?The answer is same, for the advantages of exception. a error may be thrown at bottom of the call stack. with exception or errorException you can catch in any level of call stack and show a gracefull error rather than terminating the application (incase of fatal error).Imagine a nonexistant file "require"ing breaking up your page in the middle. Notes:this is the summary of the story of error-exception for geting the concept. If there is any question and query about these, can post it in separate topic. if it needs to add something more or for any correction or improvements can PM me.the links are provided, have the depth information about what it is being posted here and posted links are meant to be followed. There is no any substitute for php manual itself.