Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> ASP.Net & Web Forms >> Exception Handling through JavaScript
Exception Handling through JavaScript

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.
Views: 1487     Comments: 0
by Arun Singh on 10/17/2011

Exception Handling through JavaScript

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. In other word, Exception handling is occurrence of unexpected code error due to which the execution of program may be terminated.

There is a specific way to handle the exception in 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 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 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>

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2592
  
Copyright © 2013MindStick. All Rights Reserved.