top of page
  • Facebook
  • Twitter
  • Instagram

Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

1

Explain exception handling in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

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 = ""; }
}

LRM_EXPORT_207556595493866_20190724_1939

Q

2

Write a note on different types of errors in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

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.

LRM_EXPORT_207556595493866_20190724_1939
bottom of page