Exception/Error Handling/Holding/Catching - with in the program Syntax errors: raises due violation of language rules while typing the code. -correct/modify the code the according to the language rule. -how to identify? - compiler/interpreter Runtime error: raises while executing the program. when a runtime a error occurs at one line of the program program terminates immediately with out executing the rest of the lines. this is called productivity loss. -due to non availability of resources either fully or partially. resources are files, memory, hardware devices, other softwares, other services etc. -due to wrong or invalid input of the data -invalid - size, type, format - violation of client business rules Logical errors: mismatch between the expected output and actual output. - verify the steps taken to solve the problem, and correct them. Controllable errors: no impact on the program , able to avoid the abnormal termination of the program. Uncontrollable errors: -hardware/harddisk crash -power Pre-Defined errors : according to language. User-Defined errors : according to client. x console.log('x : ' + x); ReferenceError: x is not defined ---------------------------------- var d = Array(-9); console.log('size of d : ' + d.length); RangeError: Invalid array length ---------------------------------- var CustomerName = 'John Miller"; console.log(CustomerName); SyntaxError: Invalid or unexpected token ---------------------------------------- var x = 10; var y = 2; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); z = x/y; console.log("x : " + x + " y : " + y + " z : " + z); ------------------------- observe the output: var x = 10; var y = 0; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); z = x/y; console.log("x : " + x + " y : " + y + " z : " + z); x : 10 y : 0 z : 0 x : 10 y : 0 z : Infinity --------------------------- console.log('start of the program : '); for(i=1;i<=5;i++) { console.log('i ' + i); } for(j=9001;j<=9005;j++) { console.log('j ' + j); } console.log('end of the program'); --------------------------------- console.log('start of the program : '); for(i=1;i<=5;i++) { console.log('i ' + i); } var m = Array(-9); // termination due to error - without executing the code after the array line console.log('code after the array statement : '); for(j=9001;j<=9005;j++) { console.log('j ' + j); } console.log('end of the program'); output: ------ i 1 i 2 i 3 i 4 i 5 C:\Users\malli\OneDrive\Desktop\JSApp\index.js:6 var m = Array(-9); ^ ^ RangeError: Invalid array length ----------------------------------- Note: as there is no any mechanism/code to catch the runtime error inside the program , program terminates abnormally without executing the rest of the code after the error occurred line. try catch throw finally Error ------------------------------------------- console.log('start of the program : '); for(i=1;i<=5;i++) { console.log('i ' + i); } try { var m = Array(-9); // termination due to error - without executing the code after the array line } catch { } console.log('code after the array statement : '); for(j=9001;j<=9005;j++) { console.log('j ' + j); } console.log('end of the program'); ---------------------------------------- case2: ------ console.log('start of the program : '); for(i=1;i<=5;i++) { console.log('i ' + i); } try { var m = Array(-9); // here an error object is created with error information // that created object is throwned from here implicitly } catch(e) { console.log(e.message); } console.log('code after the array statement : '); for(j=9001;j<=9005;j++) { console.log('j ' + j); } console.log('end of the program'); --------------------------------- start of the program : i 1 i 2 i 3 i 4 i 5 Invalid array length code after the array statement : j 9001 j 9002 j 9003 j 9004 j 9005 end of the program ----------------------------------- Note: throw - var x = 10; var y = 0; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); try { if(y==0) // any { // error as a string // error as a object throw 'y value should not be zero'; // any } z = x/y; } catch(errmsg) { console.log('using throw : '); console.log(errmsg); } console.log("x : " + x + " y : " + y + " z : " + z); -------------------------------------------------------- var x = 10; var y = 0; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); try { if(y==0) // any { // error as a string // throw 'y value should not be zero'; // any // error as a object var errobj = Error('y value should not be zero'); throw errobj; } z = x/y; } catch(obj) // obj = errobj { console.log('using throw - Error : '); console.log(errobj.message); } console.log("x : " + x + " y : " + y + " z : " + z); ------------------------------------------------------------ var x = 10; var y = 0; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); try { if(y==0) // any { // error as a string // throw 'y value should not be zero'; // any // error as a object throw Error('y value should not be zero'); } z = x/y; } catch(obj) // obj = errobj { console.log('using throw - Error : '); console.log(obj.message); } console.log("x : " + x + " y : " + y + " z : " + z); ---------------------------------------------------------- Note: raise/birth/origin of error is at one place handling of error is at some other place function Divide(a,b) { if(b==0) // any { // error as a string // throw 'y value should not be zero'; // any // error as a object // origin of error is here - but handled somewhere throw Error('b value should not be zero'); } return a/b; } var x = 10; //var y = 2; var y = 0; var z = 0; console.log("x : " + x + " y : " + y + " z : " + z); try { z = Divide(x,y); } catch(obj) // obj = errobj { console.log('using throw - Error : '); console.log(obj.message); } console.log("x : " + x + " y : " + y + " z : " + z); ------------------------------------------------------------------- const Discount = 45; console.log(Discount); Discount = 50; console.log(Discount); TypeError: Assignment to constant variable. ------------------------------------------- var Mrp = 256; var Discount = 19; var SalePrice = Mrp - Discount; console.log(SalePrice); -------------------------------- var Mrp = '256'; var Discount = 19; var SalePrice = Mrp - Discount; // string Mrp converted to number then // proceeds for any arithmetic operation console.log(SalePrice); ------------------------------------ var Mrp = 'Arjun'; var Discount = 19; var SalePrice = Mrp - Discount; // string Mrp converted to number then // proceeds for any arithmetic operation // But here conversion failed console.log(SalePrice); output : NaN ------------------------------ var Price = '69'; var Tax = 19; // in case of '+' string + anything becomes string var SalePrice = Price + Tax; console.log(SalePrice); -------------------------------- var Price = '69'; var Tax = 19; // in case of '+' string + anything becomes string //A string to convert into a number. //Converts a string to an integer. var SalePrice =parseInt(Price) + Tax; console.log(SalePrice); ------------------------------- Note: check the output ---------------------- var Price = '69.29'; var Tax = 19.59; // in case of '+' string + anything becomes string //Converts a string to a floating-point number. var SalePrice =parseFloat(Price) + Tax; console.log(SalePrice); Output: 88.88000000000001 ---------------------------------- Note: check the output ---------------------- var Price = '69.29'; var Tax = 19.59; // in case of '+' string + anything becomes string //Converts a string to a floating-point number. var SalePrice =parseInt(Price) + Tax; console.log(SalePrice); Output : 88.59 ------------------------