Error Handling

Guess the output

				
					/*
error has properties
1.message -->error message
2.name --> tells you which error name it is ...
*/

function tryCatch() {
  let result;
  try {
    result = y / 100;
  } catch (error) {
    console.log(error.message); //y is not defined
    console.log(error.name); //ReferenceError
  }
}
tryCatch();
				
			

Throw your own custom error object

				
					function tryCatch1() {
  let result;
  try {
    result = y / 100;
  } catch (error) {
    throw {
      message: "tryCatch() method while division has given this error" + error.message,
      name: "Its a custom error",
    };
  }
}
tryCatch1();

//error in console ->
// Uncaught
// {message: 'tryCatch() method while division has given this errory is not defined', name: 'Its a custom error'}
// message: "tryCatch() method while division has given this errory is not defined"
// name: "Its a custom error"
// [[Prototype]]: Object
				
			

Handle All errors

				
					//Good Practice to pull out a method and handle all types of error

function HandleAllError(error) {
  switch (error.name) {
    case "ReferenceError":
      console.log("1.Reference Error::-" + error.message);
      break;
    case "RangeError":
      console.log("2.Range Error::-" + error.message);
      break;
    case "TypeError":
      console.log("3.Type Error::-" + error.message);
      break;
    case "URIError":
      console.log("4.URI Error::-" + error.message);
      break;
    case "SyntaxError":
      console.log("5.Syntax Error::-" + error.message);
      break;
    case "EvalError":
      console.log("6.EvalError::-" + error.message);
      break;
    default:
      console.log("Error type is :" + error.name + " --> Error Message ::- " + error.message);
  }
}

//1.referenceError
const referenceError = () => {
  let finalResult = 0;
  try {
    //y is not defined
    finalResult = y * finalResult;
  } catch (error) {
    HandleAllError(error);
  }
};

//2.RangeError
const rangeError = () => {
  let diceThrow = 7;
  try {
    if (diceThrow <= 1 || diceThrow >= 6)
      throw new RangeError("Dice throw should be between 1 and 6");
  } catch (error) {
    HandleAllError(error);
  }
};

//3.typeError
const typeError = () => {
  try {
    let num = 20;
    //subString cant be done on number ..
    num.subString(1);
  } catch (error) {
    HandleAllError(error);
  }
};

//4.URIError
const uriError = () => {
  let uri = "http://%%%%%ldksjfljsdf ";
  try {
    decodeURI(uri);
  } catch (error) {
    HandleAllError(error);
  }
};

//5.syntaxError
const syntaxError = () => {
  try {
    //single quote is missing
    let confirmation = eval("confirm(' Are you sure you want to proceed)");
  } catch (error) {
    HandleAllError(error);
  }
};

//6.EvalError
//use of eval should be avoided as much as possible..
const evalError = () => {
  try {
    throw new EvalError("eval error");
  } catch (error) {
    HandleAllError(error);
  }
};

referenceError();
rangeError();
typeError();
uriError();
syntaxError();
evalError();
				
			

Leave a Comment