JavaScript Try Catch
Posted by Superadmin on May 03 2023 03:30:23

JavaScript Try Catch

By Priya PedamkarPriya Pedamkar
  

javascript try catch

Introduction to JavaScript Try Catch

The try catch statement marks a block of statements to try and specifies a response should an exception be thrown. We can declare try block with a catch or finally and nested try block in JavaScript without any complaints from JavaScript Engine.

How does Try and Catch Block Work in JavaScript?

Try block always works with either catch block or finally block. If we don’t have both then the JavaScript engine throws an error that try without a catch or finally block.

Catch block works always with the try block only. We can have multiple catch blocks with a single try block.

Syntax:

<script>
try
{
//error code
}
catch(err)
{
//error output
}
</script>

Examples of Try and Catch Block

Try is used to protect a block of code where an exception can occur. Examples of try and catch block are given below:

1. Single Try Block with Catch Block

Syntax:

<script>
try
{
//error code
}
catch(err)
{
//error output
}
</script>

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">TRY CATCH</h1>
</font>
<script>
function doTryCatch()
{
try
{
printPrime();
}
catch(err)
{
document.write("There is no such function");
document.write("<br>"+err); //prints what is the error exactly by JavaScript Engine
}
}
doTryCatch();
</script>
</body>
</html>

 Output:

JavaScript Try Catch-1.1

Code Explanation:

2. Single Try Block with Multiple Catch Blocks

3. Nested Try Catch Blocks

Whenever there is a situation if we want to catch again exception or error inside the try block. We have to go for a nested try block concept.

Syntax:

<script>
try
{
//error code
try
{
//error code
}
catch(err)
{
//error output
}
}
catch(err)
{
//error output
}
</script>

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">NESTED TRY CATCH</h1>
</font>
<script>
function doNestedTryCatch()
{
try
{
try{
printPrime();//getPrint() function calling
}
catch(err)
{
document.write(err+"<br>");
}
getAge(); //getAge() function calling
}
catch(err)
{
document.write(err);
}
}
doNestedTryCatch();
</script>
</body>
</html>

Output:

JavaScript Try Catch-1.2

Explanation:

4. Try, Catch and Finally Block

Syntax:

<script>
try
{
//error code
}
catch(err)
{
//error output
}
finally
{
//closing or kill connections code
}
</script>

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1 align="center">TRY CATCH FINALLY</h1>
</font>
<script>
function doTryCatchFinally()
{
try
{
var p=Date.parsing("January 1,2020");
}
catch(error)
{
document.write(error+"<br>");
}
finally
{
document.write("I can kill or close connection even any errors thrown in the code");
}
}
doTryCatchFinally();
</script>
</body>
</html>

Output:

Example 1.3

Explanation:

Note: We cannot declare catch or finally block without try block.

Important Errors in JavaScript

All the errors are knowing by the inbuilt JavaScript error object. It gives you an error name and error message.

1. Range Error

When we are trying to assign more than allocated value to the variable or impossible iteration, then we will come across this error.

Syntax:

var a=-6;
try{
var name="Amardeep";
name.repeat(a); //throws negative repeat number range error
}
catch(error)
{
documents.write(error.name);
}

2. Syntax Error

Predefined methods or statements used incorrectly, if you are trying to access them, then we will get this error.

Syntax:

try{
var date=Date.parsing("12-10-2019"); //no such method parsing in JavaScript so syntax error
}
catch(error)
{
documents.write(error.name);
}

3. Type Error

If you are trying to convert one type to other type but if it is not possible then we will get this error.

Syntax:

try{
var date=Date.parse("ABC"); //You can't convert ABC string to date so error throws
}
catch(error)
{
documents.write(error.name);
}

4. Reference Error

Trying to access undefined variable then we will get this error

Syntax:

var a=2,b=3;
try{
documents.write(c)//No such variable "c", it throws error
}
catch(error)
{
documents.write(error.name);
}