# Exception Handling

Exception handling is a process of handling unusual circumstances during runtime of a program. Sometimes in these circumstances, there is a need to execute an alternate set of instructions which changes the normal flow of the program.

In exception handling, there are four prominent statements

• The try statement
• The catch statement
• The throw statement
• The finally statement

### JavaScript try and catch

• The try statement tests a block of code for errors during the execution of code.
• The catch statement also has a block of code which assumes control immediately after the try block throws an error.

The JavaScript statements try and catch come in pairs:

``````try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
``````
``` <script> /*Javascript program to use exception handling and prevent the calculation of roots of a quadratic equation if root are complex Input enter the equation in the form of 1 2 1 if equation is x^2 + 2x + 1:1 1 6 Output Caught exception: roots are imaginary, so calculation stopped */ var i,t,r1,r2; var k=new Array(); function quad(t,k) { //if less than zero, throws exception if(t<0) throw "roots are imaginary, so calculation stopped"; else { //calculates the roots of the equation and display the roots document.write("roots are:"); var r1=(-1*k+ Math.sqrt(t))/2*k; var r2=(-1*k- Math.sqrt(t))/2*k; document.write(r1); document.write("</br>"); document.write(r2); } } var msg="enter the equation in the form of 1 2 1 if equation is x^2 + 2x + 1:"; //takes the values of coefficients of quadratic equation for(i=0;i<3;i++) k[i]=prompt(msg); //if b^2-4ac is less than 0, then roots are imaginary //calculate b^2-4ac t=Math.pow(k,2)-4*k*k; try { quad(t,k); } catch(err) { document.write(err); } </script> ```

### The throw Statement:

The throw statement allows us to create a customized error based on our requirement. This statement can return anything from a string to a boolean or an object.

throw "Too big";    // throw a text
throw 500;          // throw a number
``` <script> /*JavaScript program to use Exception handling and catch divided by zero, divided by power of 2 exceptions Input enter divident:9 enter divisor:2 Output Caught exception: shouldnt be divided by zero or a power of 2 */ function check(a,b) { //converts the divisor-1 value in to binary format, as 2^n-1 contains only 1''s in its binary value var c=b-1; var count=0; var converted = new Array(); while(c>=1) { converted.unshift(c%2); c = Math.floor(c/2); } for(i=0;i<converted.length;i++) { if(converted[i]==0) var count=1; } if(b==0 || count==0) { try { //throws the exception throw "shouldnt be divided by zero or a power of 2"; } //catches the exception catch (err) { document.write(err); } } else { //displays the value if no exception occurs var value=a/b; document.write(value); } } var msg="enter divident:"; a = prompt(msg); var msg="enter divisor:"; b = prompt(msg); check(a,b); </script> ```

## Error names

Eval Error: Throws this error when there is an error in the eval statement.
Range Error: Throws this error when the number used is present outside the range of accepted legal values
Reference Error: Throws a reference error when there is a reference to an undeclared variable.
Syntax error: Throws a syntax error if the syntax of the code is not proper during runtime.
Type Error: Throws this error when the type of the variable is outside the range of accepted value types.
URI Error: Throws this error if we use illegal or invalid characters in the URI.

#### Eval Error

Throws this error when there is an error in the eval statement.

#### Range Error

Throws this error when the number used is present outside the range of accepted legal values

Example:

``````<!DOCTYPE html>
<html>
<body>
<p id="demo">
<script>
var num = 1;
try {
num.toPrecision(500);
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
``````

Output:

``````RangeError
``````

#### Reference Error

Throws a reference error when there is a reference to an undeclared variable.

Example:

``````<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x;
try {
x = z + 1;
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
``````

Output:

``````ReferenceError
``````

#### Syntax Error

Throws a syntax error if the syntax of the code is not proper during runtime.

Example:

``````<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
try {
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
``````

Output:

``````SyntaxError
``````

#### Type Error

Throws this error when the type of the variable is outside the range of accepted value types.

Example:

``````<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var num = 1;
try {
num.toUpperCase();
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
``````

Output:

``````TypeError
``````

#### URI Error

Throws this error if we use illegal or invalid characters in the URI.

Example:

``````<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
try {
decodeURI("%");
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
``````

Output:

``````URIError
``````