May 29, 20222 min

JavaScript Class Notes: Exception Handling

Updated: Oct 16, 2022

Mobiprep has created last-minute notes for all topics of JavaScript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on JavaScript.

  1. JavaScript: Introduction

  2. Data Types

  3. Variables

  4. Operators

  5. Functions

  6. Conditional Statements and Loops

  7. Arrays

  8. Objects

  9. DOM Elements

  10. Cookies

  11. Pop Up Box

  12. Exception Handling

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. Explain exception handling in JavaScript.

  2. Write a note on different types of errors in JavaScript.

JavaScript Notes: Exception Handling

Question 1. Explain exception handling in JavaScript.

When an exception (or error) occurs during the execution of a program, it has to be handled to avoid interruption in the program execution. In JavaScript, exception handling is done using the try…catch…finally construct.

Throw

The throw statement is used to create an error (or throw an exception). The exception can be of string, object, number or Boolean data type.


 
Syntax: throw Exception

Example: throw "Hello";

Try…catch…finally

The try statement helps to check whether the given block of code contains any errors.

The catch block defines the block of code to be executed when an exception occurs.

The finally block is executed after the try and catch blocks.

Syntax:

<script type = "text/javascript">
 
<!--
 
try {
 
// Code to run
 
[break;]
 
}
 

 
catch ( e ) {
 
// Code to run if an exception occurs
 
[break;]
 
}
 

 
[ finally {
 
// Code that is always executed regardless of
 
// an exception occurring
 
}]
 
//-->
 
</script>
 

Example:

function myFunction() {
 
var message, x;
 
message = document.getElementById("p01");
 
message.innerHTML = "";
 
x = document.getElementById("demo").value;
 
try {
 
if(x == " ") throw "is empty";
 
if(isNaN(x)) throw "is not a number";
 
x = Number(x);
 
if(x > 122) throw "is too high";
 
if(x < 67) throw "is too low";
 
}
 
catch(err) {
 
message.innerHTML = "Error: " + err + ".";
 
}
 
finally {
 
document.getElementById("demo").value = "";
 
}
 
}
 


Question 2. Write a note on different types of errors in JavaScript.

There are three major types of errors in javascript. They are :

1) Syntax Error

The syntax error occurs during the interpret time. This error arises when there is some mistake in the syntax of the JavaScript code.

Example:

<script type=''text/javascript''>
 
window.show(;
 
</script>

The above code gives rise to a syntax error, because the closing bracket is missing (in line 2).

2) Runtime Error

The errors that occur during runtime are called runtime errors.

Example:

<script type=''text/javascript''>
 
window.show();
 
</script>

The above code raises a runtime error. Because, show() function is not defined.


 
3) Logical Error

Logical error occurs when there is a mistake in the logic of the written code. Logical errors lead to unexpected or erroneous results. An error message is not displayed for logical errors. Hence, it is difficult to identify logical errors.


Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.

Back to Top


    540
    2