Saturday, 19 September 2020

Exception handling in Javascript

 1.The try block is where you put code that could fail.

2.An error object is passed to the catch block

3.The code in a finally block always runs


syntax :

         try{

    // some code that could fail

}

catch(error){

    // Do something with the error

}

finally{

    // This code always runs

}


Note : A javascript Error object always has 'name' and 'message' property.


 ex: simple try catch

 

       let result ;

        try{

  result = x / 10;

}catch(error){

  console.log(error.message);  // x is not defined

}


  ex: Finally statement with catch block

    

      let result;

       try{

    console.log("An error will occur.");

result = x / 10;

console.log(" This line will never run.");

   } catch (error){

     console.log(" In the 'catch' block : " + error.message);

   }

       finally{

    console.log("In the 'finally' block ");

   }    


   output : 

        //An error will occur.

        //In the 'catch' block : x is not defined

        //In the 'finally' block 


 ex: Finally statement (Success)

     

         let result;

         let x = 100;

          try{

   console.log("An error won't occur.");

   result = x/10;

  } catch (error){

    console.log("In the 'catch' block : " + error.message);

  } finally{

            console.log("In the 'finally' block");

  } 


   output :

    // An error won't occur.

    // In the 'finally' block   


Throw a Custom Error Object :

1.you can throw your own custom error object.

2.Create an object with at least two properties : "message" and "name".


ex : 


   try{

     attemptDivision();

   }catch (error){

     console.log(error.message + " - Error Type: " + error.name);

   }


   function attemptDivision(){

    let result;

try{

   result = x / 10;

}catch(error){

  // Always include at least a 'message' and 'name' properties

throw{

"message" : "In the attemptDivision() method the following error occurred : " + error.message,

"name"    : "CustomError"  

};

}

   

   }

   

Types of Errors :


1.SynatxError

2.TypeError

3.ReferenceError

4.RangeError

5.URIError

6.EvalError *


ex :

 function referenceError(){

   let result ;

   try{

     // Reference error because 'x' is not defined

result = x/10;

   }catch(error){

     handleError(error);

   }

 }


 function rangeError(){

   let result=0;

   try{

     // Range error because a number cannot have 200 significant digits

result.toPrecision(200);

   } catch (error){

     handleError(error);

   }

 } 

 

 function typeError(){

  let result = 0;

  try{

    // Type error because result is a numeric

result.toUpperCase();

  } catch(error){

    handleError(error);

  }

 

 }

 

 function uriError(){

   let uri = "http://www.netinc.com/path%%%/file name";

   

   try{

    // URI eror

decodeURI(uri);

   } catch (error){

     handleError(error);

   }

 }

 

 function synatxError(){

   try{

   // Syntax error because missing a final single quote

    let sum = eval("alert('Hello)");

   } catch (error){

     handleError(error);

   }

 }

 

 function handleError(error){

   switch(error.name){

    case 'ReferenceError':

console.log("Reference error : " + error.message);

     break;

case 'RangeError':

  console.log("Range error : " + error.message);

      break;

case 'TypeError':

  console.log("Type error : " + error.message);

      break;

case 'URIError':

  console.log("URI  error : " + error.message);

      break;

case 'SynatxError':

  console.log("Syntax  error : " + error.message);

      break; 

    case 'EvalError':

  console.log("Evaluation  error : " + error.message);

      break;

    default :

    console.log("Error Type : " + error.name + " - Message: " + error.message);

break;

   } 

 }


No comments:

Post a Comment