Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i handle errors and continue function ? Javascript js

function mix(){
    //console.log(arguments)
    let answer;
    let errorsArr=[];
    let errorNew;
    iterations: for( let i = 0 ; i < arguments.length; i++){
        if(typeof arguments[i]!=='function'){
            throw new Error('Every argument has to be function!')
        }else{
                try {
                    answer = arguments[i](answer); 
                    
                } catch (error) {
                    errorNew = {name:`${error.name}`,message : `${error.message}`,stack:`${error.stack}`,level:`${error.level}`}
                    errorsArr.push(errorNew)
                   
                } finally{
                    continue iterations;
                }

        }
    }
    const returnedObject = {
        errors :errorsArr,
        value: answer
    }

    return returnedObject;
    
}


function square(n){
    return n*2
}
function million(){
    return  1000000
}
function divideTwo(n){
    return n/2;
}


console.log(mix(million,square,divideTwo))

I had a homework to do a mix function to an unlimited arguments. it works perfectly if all arguments are functions but when any kind of error occurs it should handle and skip errored iteration without breaking program. how can I handle it correctly?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Its the issue with your error handling section.

You should wrap your typeof arguments[i] !== "function" statement inside a try catch because its that place where you are throwing the error

function mix() {
  console.log(arguments);
  let answer;
  let errorsArr = [];
  let errorNew;
  iterations: for (let i = 0; i < arguments.length; i++) {
    try {
      if (typeof arguments[i] !== "function") {
        throw new Error("Every argument has to be function!");
      } else {
        answer = arguments[i](answer);
      }
    } catch (error) {
      errorNew = {
        name: `${error.name}`,
        message: `${error.message}`,
        stack: `${error.stack}`,
        level: `${error.level}`,
      };
      errorsArr.push(errorNew);
      continue iterations;
    }
  }
  const returnedObject = {
    errors: errorsArr,
    value: answer,
  };

  return returnedObject;
}

function square(n) {
  return n * 2;
}
function million() {
  return 1000000;
}
function divideTwo(n) {
  return n / 2;
}

console.log(mix(million, 5, square, divideTwo));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading