Users Online

· Guests Online: 47

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

JavaScript Try Catch

JavaScript Try Catch

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.

  • Try Block: Try block is used for writing error code like not divisible by zero, array index out of bound exception, etc.
  • Catch Block: Catch block is used for catching or writing error output.
  • Finally Block: Finally block is used for writing a very important code like kill the connection, close the connection, etc. It will execute even we have an error within the try block.

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>
  • We can have a nested try block in JavaScript.
  • We can declare multiple catch blocks with a single try block without any error from JavaScript Engine.

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:

  • In the try block, we are trying to call printPrime() but don’t have that method. So, here catch the exception with a catch block.
  • Catch block throws User-defined(There is no such function) and Actual error(err=>ReferenceError: PrintPrime is not defined).

2. Single Try Block with Multiple Catch Blocks

  • It is a little bit interesting to know that we don’t have a single try with multiple catch blocks in JavaScript. If we arise such a case, we can achieve it through if-else statements or switch case.

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:

  • I have a situation unknown method inside try block twice. So, I don’t want to throw it single catch block then I have taken one more try and catch block inside try block.
  • It gives first catch block output because of no printprime() method, then second catch block output because of no get() method.

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:

  • Try block throws an error and catch block finds that we don’t have parsing method with Date class.
  • Finally, block automatically executes even we have errors, so it executes output.
Note: We cannot declare catch or finally block without try block.
  • While we are dealing with a try, catch and finally statements, we must know some errors thrown by try-catch in JavaScript.

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

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.75 seconds
10,842,572 unique visits