---
title: "Exception Handling in JavaScript"  
description: "Exception handling is an important concept which is widely used in programming language. Exception handling is an important approach which is design"  
author: "Anonymous User"  
published: 2011-08-26  
updated: 2020-06-02  
canonical: https://www.mindstick.com/articles/619/exception-handling-in-javascript  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Exception Handling in JavaScript

[Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is an important concept which is widely used in [programming language](https://www.mindstick.com/articles/332896/top-5-programming-languages-for-ios-app-development). Exception handling is an important approach which is designed to handle unexpected occurrence of code error in programming language. That is, we can say that in other word exception handling is occurrence of unexpected code error due to which the execution of program may be terminated.

There are lots of situation occurs where we have to use exception handling concept such as suppose that we are browsing a [web pages](https://answers.mindstick.com/qa/95530/how-do-i-fix-safari-not-loading-web-pages) and when we click any control on [web page](https://answers.mindstick.com/qa/92898/how-to-create-a-web-page-using-bootstrap-4) then an [alert box](https://www.mindstick.com/articles/12940/how-to-create-message-alert-box-using-jquery) open and telling us this runtime error (due to any reason may be css not loaded, may be any file not loaded etc.) and message shows ‘Due want to debug it’ if user are developer then it is possible to he would be continue otherwise a naïve user close the web page. So in this situation we have to use exception handling within the [javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) code block.

There is a specific way to [handle the exception](https://www.mindstick.com/forum/159349/who-and-how-handle-the-exception-when-we-use-throws) in program. Try, catch and finally are used to handle the exception. Try contains those blocks of code there is probability to occurrence unexpected code error and when exception is raised then [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block) catches that exception and handle that exception i.e catch block contains exception handler code.

Here I am giving a small example to how exception handles within the javascript code. Here I am taking two input number1 and number2 and print division result of these two numbers.

![Exception Handling in JavaScript](https://www.mindstick.com/mindstickarticle/f5b1f68a-0a06-4d13-8906-f4daa3fedfe2/images/af2bf235-0e2b-4b63-a6aa-8b062cf80347.png)

To design the above UI write the following code in .aspx page.

```
<form id="form1" runat="server">     <div>     <table style="width: 446px;  height: 132px">       <tr>         <td class="style1">          <asp:Label ID="Label1" runat="server" Text="Enter first Number:"></asp:Label>         &nbsp;         </td>         <td>         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"                 ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>             <asp:RangeValidator ID="RangeValidator1" runat="server"                 ControlToValidate="TextBox1" ErrorMessage="*" MaximumValue="999999"                 MinimumValue="0" Type="Integer"></asp:RangeValidator>         </td>       </tr>        <tr>         <td class="style1">          <asp:Label ID="Label2" runat="server" Text="Enter Second Number:"></asp:Label>         </td>         <td>         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"                 ControlToValidate="TextBox2" ErrorMessage="*"></asp:RequiredFieldValidator>             <asp:RangeValidator ID="RangeValidator2" runat="server"                 ControlToValidate="TextBox2" ErrorMessage="*" MaximumValue="999999"                 MinimumValue="0" Type="Integer"></asp:RangeValidator>         </td>       </tr>       <tr>          <td class="style1">                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                   </td>          <td>         <%-- ClickEventResult Call the javascript block when button clicked by client--%>                <asp:Button ID="Button2" runat="server" Text="Division" OnClientClick="ClickEventResult();" />                      </td>       </tr>     </table>                                 </div>     </form>
```

##### The javascript for button click event is :

```
<script type="text/javascript">         functionClickEventResult() {             var num1=document.getElementById("TextBox1").value;             var num2=document.getElementById("TextBox2").value;               try {                 if (num1== ""||num2== "") {                    //// throw the exception when textbox is empty, throw keyword is used for throw user execption                     throw "empty";                }                 else {                 /// calculate division result                      var result;                    result=num1/num2;                    //// result show with confirm pop up message box                     confirm(result);                }              }             catch (Error) {                 if (Error== "empty") {                     /// exception catch when empty throw                     alert("Textbox cannot empty:"+Error);                }                   else {                 //// exception occured when result calculated                     alert(Error);                }            }                                           }           </script>
```

When we input two number and first number is divided by 0 then exception is raised and there one alert box show the exception error.

![Exception Handling in JavaScript](https://www.mindstick.com/mindstickarticle/f5b1f68a-0a06-4d13-8906-f4daa3fedfe2/images/0a0692e8-1eb9-4331-a3ef-74e68ca93052.png)

![Exception Handling in JavaScript](https://www.mindstick.com/mindstickarticle/f5b1f68a-0a06-4d13-8906-f4daa3fedfe2/images/3a9474ae-844d-49b4-9bca-a431cdc91239.png)

\

---

Original Source: https://www.mindstick.com/articles/619/exception-handling-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
