Suggested Videos
Part 28 - JavaScript closure example
Part 29 - JavaScript arguments object
Part 30 - Recursive function in JavaScript
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 :
Output :
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 :
Output :
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.
JavaScript throw statement : Use the throw statement to raise a customized exceptions.
JavaScript throw exception example :
Part 28 - JavaScript closure example
Part 29 - JavaScript arguments object
Part 30 - Recursive function in JavaScript
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 :
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");
}
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();
Hello sir I typed the same code but the throw ( custom message is not written on the document .
ReplyDeleteAnd window.on error event is also not printing the error if there is the error in the function syntax please reply
Hi Tahir Zaman,
ReplyDeleteplease add debugger then hopyfully you will know that why custom message not written
eg - please enter - numerator as 0 and denominator as positive integer value then will get custom message