This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube
SOURCE Address here:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
Use try/catch/finally in JavaScript to handle runtime errors. These runtime errors are called exceptions. A variety of reasons can lead to exception. For example, Using variables or methods that are not declared can cause exception.
JavaScript statements that may cause exceptions should be wrapped in a try statement. When a sentence within a try statement causes exception, control is immediately transferred to the catch statement, and then all the remaining code content within the try statement is skipped.
Javascript Try catch Example:
Try { //referencing a function that does not exist cause an exception//using a non-existent function results in a exception document.write (SayHello ()); //Since The above line causes a exception, the following line is not being executedBecause the above code causes exception to occur, the following code will not run
document.write ("This line won't be executed");}//When a exception occurs, the control is transferred to the CATCH blockWhen exception is generated, control is transferred to the catch statement block.
Catch(e) {document.write ("Description =" + E.description + "[br/]"); document.write ("Message =" + E.message + "[br/]"); document.write ("Stack =" + E.stack + "[br/][br/]");} document.write ("This line would be executed");
Output://JavaScript Try Catch Example.png
Note: A try statement block should always be followed by a catch statement block or a finally statement block, or both
The finally statement block runs regardless of whether the exception is generated. Its main use is to clean up and release resources that are used during the script's run. For example, if your try statement block opens a file and runs, and a exception occurs, Then the finally statement block is used to close the file.
Javascript Try Catch finally example:
Try { //referencing a function that does not exist cause an exceptiondocument.write (SayHello ()); //Since The above line causes a exception, the following line is not being executeddocument.write ("This line won't be executed");}//When a exception occurs, the control is transferred to the CATCH blockCatch(e) {document.write ("Description =" + E.description + "[br/]"); document.write ("Message =" + E.message + "[br/]"); document.write ("Stack =" + E.stack + "[br/][br/]");}finally{document.write (Guaranteed to execute);}
Output:javascript Try Catch finally Example.png
The formatting errors and exceptions in JavaScript need to be noted:
try/catch/finally statement blocks can snap to exceptions, but they cannot be captured by formatting errors.
Example: There is a format error in the following code-the closing parenthesis is missing. The catch statement block cannot catch this error
Try { alert ("Hello";} Catch (e) { document.write ("JavaScript syntax errors cannot is caught in the catch block"); }
Javascript Throw statement: Use the throw statement to capture the custom exceptions
Javascript Throw Exception Example:
JavascriptThrowException Example:functiondivide () {varNumerator = number (prompt ("Enter numerator")); varDenominator = number (prompt ("Enter denominator")); Try { if(Denominator = = 0) { Throw{error:"Divide by zero error", message:"Denominator cannot be zero" }; } Else{alert ("Result =" + (Numerator/denominator)); } } Catch(e) {document.write (E.error+ "[br/]"); document.write (E.message+ "[br/]"); }}divide ();
Translation Error handling in JavaScript (err handling)