Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
Error handling in JavaScript
Play lesson

JavaScript Tutorial - Error handling in JavaScript

4.0 (0)
13 learners

What you'll learn

This course includes

  • 12.3 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

Link for all dot net and sql server video tutorial playlists http://www.youtube.com/user/kudvenkat/playlists Link for slides, code samples and text version of the video http://csharp-video-tutorials.blogspot.com/2015/01/error-handling-in-javascript.html Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Use try/catch/finally to handle runtime errors in JavaScript. These runtime errors are called exceptions. An exception can occur for a variety of reasons. For example, referencing a variable or a method that is not defined can cause an exception. The JavaScript statements that can possibly cause exceptions should be wrapped inside a try block. When a specific line in the try block causes an exception, the control is immediately transferred to the catch block skipping the rest of the code in the try block. JavaScript try catch example : try { // Referencing a function that does not exist cause an exception document.write(sayHello()); // Since the above line causes an exception, the following line will not be executed document.write("This line will not be executed"); } // When an exception occurs, the control is transferred to the catch 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 will be executed"); Output : // JavaScript try catch example.png Please note : A try block should be followed by a catch block or finally block or both. finally block is guaranteed to execute irrespective of whether there is an exception or not. It is generally used to clean and free resources that the script was holding onto during the program execution. For example, if your script in the try block has opened a file for processing, and if there is an exception, the finally block can be used to close the file. JavaScript try catch finally example : try { // Referencing a function that does not exist cause an exception document.write(sayHello()); // Since the above line causes an exception, the following line will not be executed document.write("This line will not be executed"); } // When an exception occurs, the control is transferred to the catch block catch (e) { document.write("Description = " + e.description + "[br/]"); document.write("Message = " + e.message + "[br/]"); document.write("Stack = " + e.stack + "[br/][br/]"); } finally { document.write("This line is guaranteed to execute"); } Output : JavaScript try catch finally example.png Syntax errors and exceptions in JavaScript try/catch/finally block can catch exceptions but not syntax errors. Example : In the example, below we have a syntax error - missing the closing parentheses. The associated catch block will not catch the syntax errors. try { alert("Hello"; } catch (e) { document.write("JavaScript syntax errors cannot be caught in the catch block"); } JavaScript throw statement : Use the throw statement to raise a customized exceptions. JavaScript throw exception example : function divide() { var numerator = Number(prompt("Enter numerator")); var denominator = 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();

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere