Professional Documents
Culture Documents
Error Handling in Javascript: Sebin Benjamin
Error Handling in Javascript: Sebin Benjamin
Edsger W. Dijkstra
Types of Errors
When developing programs there are several types of error that can occur.
• Syntax Errors
• Semantic Errors
• Logic Errors
• Arithmetic Errors
• Compile time/static error
• Run time/Dynamic Errors
• Resource Errors
• Interface Errors
Syntax Errors
• Syntax errors are errors that occurs when the pre-defined syntax (rules) of
the language is not respected.
• Syntax errors are detected while interpreting or parsing the source code.
• The compiler/interpreter will give you the location of the syntax error,
which makes it rather easy to find/fix.
• In English, a convoluted example would be, “THE dog bit, the mahn.”
• Example,
console.log("Hello World);
Semantic Errors
• A semantic error is one related to the meaning of something.
• These are errors due to an improper use of program statements.
• Some semantic errors are detected by the computer, but some remain
hidden to produce unexpected errors.
• In English, it might be a conversation like, “Bit me the dog
did”.
• Example: Wrong order of statements - trying to use a variable without
assigning it a value.
const x = y + 5;
// Uncaught ReferenceError: y is not defined
Logic Errors
• Logical errors are caused when the requirements are not met. The
program is run without any errors but does not generate the
requested result.
• Your computer will do EXACTLY what you told it to do.
• Often, the program you wrote is not the program you wanted to
write.
• Logical errors are the hardest of all error types to detect.
• In English, this is what might happen when you might have intended
to say, “A dog bit a man” but end up with, “A man bit a dog”.
Arithmetic Errors
• An arithmetic error is a type of logic error that involves mathematics.
• Usual examples include, division by 0, but not in JavaScript.
• Wrong math concepts and formulas could be included in this.
• Functional tests that always include edge-cases like zero, or negative
numbers is an one way to stop these arithmetic errors in their tracks.
// Adds two numbers and returns the sum.
function addNumbers(firstNumber, secondNumber)
{
return firstNumber - secondNumber;
}
$125
million
https://www.simscale.com/blog/2017/12/nasa-mars-climate-orbiter-metric/
Compile-time errors
• Computers can only understand machine code, int x = 5;
which is a low-level programming language*,
generally being made up entirely of numbers
(0’s & 1’s).
• Compiling is converting programming language
code to machine code. Some programming
languages like C, C++, C# Java, Go require a 01101000 01100101
compilation step, before it can be executed. 00100000 01110111
https://stackoverflow.com/questions/95635/what-does-a-just-in-time-jit-compiler-do
Runtime errors
• A runtime error is the one that occurs unexpectedly when your
software is executed, or while it’s running
• Often software will most likely work well on your end, and the
compiler won’t flag any abnormalities, but mess up when the end-
user runs it.
• Example,
display('Hello World’);
// Uncaught ReferenceError: display is not defined
Resource errors & Interface errors
• These are a type of runtime error that could occur.
• Resource errors occur when a resource could not be accessed.
• Example, when you exceed the fixed amount of resources allocated to the program
or try to read a file without proper access permissions.
• Interface errors occur when there is a disconnect between how you meant
your program to be used and how it is actually used.
• Example, if you have an API that requires that specific parameters are set and those
parameters are not set.
• Having clear documentation and catching these errors to pass back to the caller in a
useful way is the best way of saying, “Hey, you haven’t given us what we need to
process this request.”
Handling Error
Avoiding errors, using validation for external systems, try-catch-finally
Avoiding Errors
• A JavaScript linter or good editor (VS Code) can catch syntax errors
such as misspelled statements and missing brackets.
• You can use validation to catch errors in data from users or other
systems.
• Example: client-side validation in the UI, server side validation to check the
API params.
• However, some runtime errors are impossible to avoid
• the network can fail
• a busy server or application could take too long to respond
• a host’s infrastructure can fail
Showing errors to the user
• Showing an error to the user is the last resort.
• Some are useful
• The file already exists. Would you like to overwrite it?”
• Some are NOT very useful
• Something went wrong !
• You may be able to ignore some less-critical errors such as an image failing to
load
• Best way is to ensure the user can take some appropriate actions.
https://blog.openreplay.com/an-introduction-to-javascript-error-handling
Error Handling in JavaScript
• When an error occurs JavaScript creates an object and hands it off to the runtime.
• An error in JavaScript is an object, which is later thrown to halt the program.
• An error is thrown to raise an exception (exceptional event).
• An error object becomes an exception only when it's thrown.
• Exceptions can be caught and handled later.
• The runtime system searches the call stack for a method that contains a block of code that can handle
the exception. This block of code is called an exception handler
• You can catch exceptions in a try ... catch block.
• This executes the code in the try {} block but, when an exception occurs, the catch
{} block receives the object returned by the throw.
• You can define an optional finally {} block if you require code to run whether the
try or catch code executes
Catching an exception
• Exceptions are like an elevator going up: once you throw one, it
bubbles up in the program stack, unless it is caught somewhere.
• When, and where you catch an exception in your code depends on
the specific use case.
• Read more : https://www.valentinog.com/blog/error/
try...catch...finally
• The try...catch statement is comprised of try {
a try block and either a catch block, a try_statements
finally block, or both. }