1.What is Error?

Shamim AL-Mamun
3 min readMay 6, 2021

--

Every programmer in the world is familiar with the word is “Error”. From beginner to expert, a programmer always has been the victim of this trouble. It is a problem that occurs in the program. Today, I will talk about handling of error that is “try…catching”. Here, try means test a block of codes for checking errors. And catch statement helps to handle the error.

2. Do You know try…catch only works for runtime errors?

A program during execution is called runtime errors. The main purpose of try…catch to work, the code must be runnable.

try {
{{{{{
} catch (err) {
alert(“Error has occured”);
}

3. Error Object

Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions

When an error occurs, JavaScript generates an object that are passed as an argument to catch.

try {
// code
} catch (err) { //error object
// code
}

4.Own errors

When we develop something, we often need our own error classes to reflect specific things that may go wrong in our tasks. JavaScript allows to use throw with any argument

Sometimes, you see that json is syntactically correct but you don’t have a occupation property. See this.

let json = ‘{ “age”: 23 }’;try {
let user = JSON.parse(json); // ← no errors
alert( user.occupation );
} catch (err) {
alert( “no execution” );
}

5. try…catch…finally

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block. The finally statement lets you execute code, after try and catch, regardless of the result.

finally statement used to execute the code after try and catch. The code is:

try {
// code
} catch (err) {
// code
} finally {
// code
}

6. stanceof operator

An instanceof in Java is a comparison operator which, given an object instance, checks whether that instance is of a specified type (class/sub-class/interface) or not. Just like other comparison operators, it returns true or false.

We can check the type of error by using instanceof operator.

try {
varA = { /*…*/ };
} catch (err) {
if (err instanceof ReferenceError) {
alert(‘Error has occured’);
}
}

Reference error mainly used for undefined variable.

7. The “try…catch” syntax

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

There are two blocks in “try…catch”, try and catch.

try {
// code testing process
} catch (err) {
// error handling process
}

From the flowchart:

1. Firstly, try statement is executed.

2. If no errors occurred, it simply ignores catch statement but if errors occurred, it ignores try statement and execute catch statement.

8. Using “try…catch”

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

Let’s go to a real life example, JSON. We mainly used to get the decoded data by using JSON parsing. A simple example of JSON is:

json = ‘{“name”:”Mahmudur”, “age”: 23, “occupation”: “Web Developer”}’;

9. “catch” binding

The optional catch binding is a new feature in JavaScript that was added in the ES10/2019, this feature is useful in some cases when you don’t need the binding parameter in the catch clause.

If we don’t need any details information about error, catch may ignore it.

try {
// code
} catch { // without err
// code
}

--

--

Shamim AL-Mamun
0 Followers

Dynamic and detail-oriented Web Developer with a skill for user-friendly, solutions effectively. I have an extensive understanding of Front-end and Back-end.