Open In App

How to throw an error when using a property of an object ?

In this article, we will try to understand how we may throw an error when using a property of an object with the help of certain theoretical explanations as well as coding examples in JavaScript.

Let us first have a look over the below section which shows the syntax for creating an object having certain properties and their corresponding values in JavaScript.



Syntax:

let object_name = {
    property_name : value,
    ...
}

Now, let us have a quick look over the below example which shows the creation of an object and also how to access any property of an object in JavaScript.



Example: In this example, we will create an object which will contain simple property-value pair along with the nested object’s property-value pairs, and try to access any property from it and display it in the output.




<script>
    let employee_details = {
        name: "Aman",
        designation: "technical writer",
        age: 22,
        graduated: true,
        address: {
            street: "XYZ",
            place: "ABC",
            state: "PQR",
            pincode: 1122333,
        },
        skills: {
            coding: ["C", "C++", "Java"],
            frontEnd_BackEnd: ["HTML", "CSS"
                "JavaScript", "React", "TypeScript"],
        },
    };
  
    console.log(employee_details);
  
    console.log(
        "Place where an employee lives is : " 
            + employee_details.address.place
    );
</script>

Output:

{
  name: 'Aman',
  designation: 'technical writer',
  age: 22,
  graduated: true,
  address: { street: 'XYZ', place: 'ABC', 
      state: 'PQR', pincode: 1122333 },
  skills: {
    coding: [ 'C', 'C++', 'Java' ],
    frontEnd_BackEnd: [ 'HTML', 'CSS', 
        'JavaScript', 'React', 'TypeScript' ]
  }
}
Place where an employee lives is : ABC

Now let us have a look over our main problem statement, which is how to throw an error when using a property of an object with the help of certain examples shown below:

Example 1: 




<script>
    let employee_data = {};
    Object.defineProperty(employee_data, "name", {
        get: function () {
            throw new Error("Employee Data not found!");
        },
    });
  
    try {
        let data = employee_data.name;
        console.log(data);
    } catch (error) {
        console.log("Caught Error: " + error.message);
    }
</script>

Output:

Caught Error: Employee Data not found!

Example 2: 




<script>
    let employee_data = new Proxy(
        {},
        {
            get(object_name, property_name) {
                throw new Error(`${property_name} 
                    property not found!!..`);
            },
        }
    );
  
    try {
        let property_1 = employee_data.age;
        console.log(property_1);
    } catch (error) {
        console.log(error.message);
    }
  
    try {
        let property_1 = employee_data.name;
        console.log(property_1);
    } catch (error) {
        console.log(error.message);
    }
</script>

Output:

age property not found!!..
name property not found!!..

Example 3: 




<script>
    let employee_details = {
        name: "Aman",
        age: 22,
    };
  
    let checkProperty = (propertyName, object) => {
        return propertyName in object ? true : false;
    };
  
    try {
        if (checkProperty("address", employee_details) === false)
            throw new Error("Property not found..!!");
    } catch (error) {
        console.log("Caught Error: " + error.message);
    }
</script>

Output:

Caught Error: Property not found..!!

Article Tags :