codescracker


javascript

JavaScript try catch throw finally



« Previous Tutorial Next Tutorial »


You can use try-catch statement to write your JavaScript code in professional manner. Using the try-catch statement in JavaScript means writing the JavaScript code, which can generate an error, in the try block. Immediately, after the try block, there is a catch block that specifies the exception type that you want to catch.

Here is the general form of the try-catch statement in JavaScript:

try
{
	// JavaScript code that may cause an error
}
catch
{
	// code to execute on finding some errors in the code 
	// written in try block
}

You can use JavaScript throw statement to define a custom error. Here is an example of JavaScript throw statement:

throw "It is too much";
throw 300;

You can use JavaScript finally block to execute the code placed inside the finally block, regardless whether the error occurred or not. Here is the general form of the JavaScript finally block:

try
{
    // JavaScript code to be executed first
}
catch(err)
{
    // JavaScript code to handle errors caused in the code of try block
}
finally
{
    // JavaScript code to execute regardless the result of try-catch block
}

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test