简体   繁体   中英

How to throw an error using try catch statement?

//Utility
  const readUserUtility = (id_a)=>{
    const currentstore = store.getState();
    const returnedArray = currentstore.users.filter((user_object)=>{
        return user_object.id === id_a;
    })

    try{if(returnedArray.length === 0){throw "Array empty";}}catch(error){
    console.log(error);


    }

    console.log(returnedArray);
  }

I have this function readUserUtility as part of a redux application. When i call readuserUtility with an integer im getting the error: Line 144: Expected an object to be thrown no-throw-literal

The message "Array empty" still shows up in the console, but why am i getting this error?

You have to create a new Error object and pass that. Below works for me:

throw new Error("item not found");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM