---
title: "Exception Handling through JavaScript"  
description: "Exception handling is an important concept which is widely used in programming language. Exception handling is an important approach which is designed"  
author: "Anonymous User"  
published: 2011-10-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/256/exception-handling-through-javascript  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Exception Handling through JavaScript

[Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is an important concept which is widely used in programming language. Exception handling is an important approach which is designed to handle unexpected occurrence of code error in [programming language](https://www.mindstick.com/articles/329035/5-most-in-demand-programming-languages-to-consider-for-your-app-development-in-2022). In other words, Exception handling is the occurrence of unexpected code error due to which the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of program may be terminated.\

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 [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript). 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.

**Example:** How to [handle exception](https://www.mindstick.com/forum/162065/how-to-handle-exception-globally-in-dot-net-core) through the JavaScript

```
<html><head><title>Exception Handling</title> <script type="text/javascript">    function ClickEventResult() {        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></head><body>    <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>  </body></html>
```

---

Original Source: https://www.mindstick.com/blog/256/exception-handling-through-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
