Jump to content

All about PHP error-exception

Recommended Posts

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
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.
find the directives here
"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.ini
Error types:
  • Syntactical error
  • Runtime error
  • Logical error

Syntactical error:
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.
here 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 cure
You can avoid much of parse error if you write well readable code followinga any code standard. like
and using decent IDE(eg netbeans) or text editor(eg notepad++)
Runtime Error:
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, etc
how 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.
: It level is lower than Fatal error. but is origin of unexpected behaviour even can lead to fatal error.
: Low level error. does not harm much. but can lead to higher level error. so
Logical error:
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
. there is
for php which can also help you in that.
How would i know so many different error?
Runtime errors are
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.
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.
there is another option, recruite a error handler which will handle all the error as YOU WANT.
I want to throw my own error. how to?
there is
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
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)
Why we will use exception? why php API uses exception (eg PDO)?
PHP already documented exception in their
. 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 customaclly
We 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.
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. Edited by birbal
  • Like 1
Link to post
Share on other sites

Amen bibal. I'm adding this to my PHP references. EDIT We can't have too many reminders on error reporting. Just like we can't be reminded too many times to wear our seat belts and drive safely. Good post.

Edited by niche
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...