Error handling in JavaScript04:33

  • 0
Published on April 5, 2017

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

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();

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

depo 25 bonus 25

https://parfumschristianblanc.com/

https://www.barplate.com/wp-includes/js/qris/

https://hotmusic507.org/

Enjoyed this video?
"No Thanks. Please Close This Box!"