Loading...

Exception Handling in JavaScript

Exception Handling in JavaScript

Coders make mistakes that are in the form of errors or exceptions. Dealing with these exceptions is an issue every programmer faces. It is inevitable! If you are just starting with coding or are not aware of how to deal with errors, we got you! Here we will discuss exception handling in JavaScript, the different types of errors you might encounter, and how to deal with those errors. This topic is fascinating and interactive, but it is recommended that you have some basic knowledge of HTML, CSS, and JavaScript.

What is Exception Handling in JavaScript?

Exception handling in JavaScript is one of the most powerful features this programming language provides us. It helps us to deal with errors and the sudden crashing of our JavaScript code. Therefore it allows us to maintain a regular program flow which is very important for developers. Exceptions are just an explanation of what went wrong in the code. It also helps in tracking the error. Errors could be mistakes made by the programmer, wrong input/logic (semantics), incorrect syntax, etc.

Here are some reasons for exceptions to occur:

  • Dividing a number by zero throws an error
  • Accessing a file that doesn’t exist in the system
  • Not handling an incorrect input well, provided by the user
  • Network inaccessibility during communication

When JavaScript comes across an error it raises an exception. The JavaScript compiler then looks for an exception handling program in the proceeding program. If there is then the program does not crash due to that error. Note that exceptions can be dealt with in the programming environment but errors can’t be.

Types of Errors

Many sorts of errors can occur while running or compiling JavaScript code. Mainly there are three types of errors, which are as follows:

  1. Syntax Errors
  2. Runtime Errors
  3. Logical (semantic) Errors

Now, let us understand in-depth about these errors one by one.

1. Syntax Errors

These errors can not be understood by the interpreter of the computer. Syntax errors are raised during the compilation of the code. This includes spelling errors of identifiers or keywords, for instance, writing ‘function’ as ‘fiction’ or writing ‘let’ as ‘lwt’ and so on. Other reasons could include the wrong indentation or forgetting to use semicolons at the end of a statement.

2. Runtime Errors

These errors occur during the running or execution of the code. These errors get detected only when you run the program. It results in the sudden crashing of the program. This is where exception handling comes to the rescue. Some reasons for getting runtime errors could be that the program is unable to find data that does not exist or the input given has the wrong data type which is not checked initially. These errors commonly occur in APIs. Due to human error, there are often cases when the user input is not validated properly inside the API. And to prevent the APIs from crashing, exception handling plays a major role.

3. Logical Errors

The last types of error are logical error. These errors do not throw any exceptions at all. They are caused when the program gives an unexpected output or does not work as per the programmer’s intention. It is difficult to find these sorts of errors and they can not be solved through exception handling. They can be found and resolved only by thoroughly testing the code.

What are error objects?

Every time a runtime error occurs, the code stops abruptly and JavaScript raises an exception with an error object. This error object has two main properties, error name, and error message. Error name stores the name of the error. While error messages describe the error in the form of a string. There are six types of error objects and they lay the foundation of exceptional handling. They are –

  1. EvalError
  2. RangeError
  3. ReferenceError
  4. SyntaxError
  5. TypeError
  6. URIError

Now, let us understand these exceptions one by one.

  1. EvalError: This error indicates an issue with the arguments of eval() function. This function is global but now JavaScript does not raise an exception for this anymore. By the way, you must avoid using eval() function at all costs.
  2. RangeError: This error is thrown when a numerical value is outside a defined range.
  3. ReferenceError: This error is thrown when there are undeclared variables being used.
  4. Syntax Error: Exception occurs when a JavaScript rule is broken or when the programmer misuses the syntax.
  5. TypeError: Error occurs when a different unexpected value is given.
  6. URIError: Exception thrown by the encodeURI and decodeURI functions.

Exception Handling

To prevent our code from crashing we need to make use of exception handling. And so we have try and catch blocks. The try block runs the code where there might be an exception. Then the catch block catches the exceptions being thrown. One can make use of if-else statements as well but try-catch blocks provide more benefits. For instance, you have the finally clause and the throw statement. The finally block is the code that always gets executed whatsoever and the throw statement is to generate your exceptions. This is way too helpful in data validation and APIs in genneral.

The try-catch syntax

try{
//..... Code you want to execute
} catch{
//..... Executed when there is an exception in try block
} finally{
//..... Always gets executed
}

Conclusion

Exception handling is one of the most important concepts every programmer should know about. We have discussed the types of errors in JavaScript and how to handle exceptions. We have also curated a list of the topmost JavaScript errors and how to solve them as well. If you are fond of such content then do not forget to check out Codedamn!

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far