Duckett Ch. 10 Error Handling and Debugging
Order of Execution
- understanding the order in whoch things are processed
Execution Context
- Every statemnet in a script live in one of three places
- Global context
- Function Context
- Eval Context
Error Objects
- Error : Generic error
- SyntaxError: Syntax has not been followed
- ReferenceError: Tried to reference a variable that is not declared/within scope
- TypeError: An unexpected data type that can not be coerced
How to Deal With Errors
- Debug the script to fix errors
- use developer tools
- Handle Errors Gracefully
- using try, catch, throw and finally statements
Debugging Workflow
- Where is the problem?
- Look at the error message, it tells you
- Check how far the script is running
- The type of error
- What exactly is the problem?
- When you have set breakpoints, you can see if the variables arround them have the values you would expect them to.
- Break down/break out parts of the code to test
- Check the numbe of parameters for a function or the number of items for an array
Summary
-
If you understand execution contexts (which have twostages) and stacks, you are more likely to find the error in your code.
-
Debugging is the process of finding errors. It involves a process of deduction.
-
The console helps narrow down the area in which the error is located, so you can try to find the exact error.
-
JavaScript has 7 different types of errors. Each creates its own error object, which can tell you its line number and gives a description of the error.
-
If you know that you may get an error, you can handle it gracefully using the try, catch, finally statements. Use them to give your users helpful feedback.