Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

JavaScript Exception Handling – Part II

By Exforsys | on August 2, 2007 |
JavaScript Tutorial

JavaScript Exception Handling – Part II

In this JavaScript tutorial, you will learn about exception handling viz. try…catch..finally statement and nested try…catch statements along with syntax and examples.

try…catch..finally Statement:

JavaScript has a finally statement that can be used as an optional construct along with try..catch statements. The finally construct placed in a try…catch construct is an optional statement. When the finally statement is placed in a try…catch construct, it always runs following the try…catch structure.

The general syntax of a try…catch..finally statement is as follows:


try
{
…………      
//Block of code which is to be tested for errors
…………
}
catch(err)
{
…………
…………     
//Block of code which is to be executed if an error occurs
}
finally
{
…………
…………    
//This is optional and runs after try and catch structure has run.
}

 

for example:

 


<html>
   <
head>
      <
script type="text/javascript">
        try
        {
           document.write(junkVariable)
        }

        catch(err)
        {
           document.write(err.message + "<br/>")
        }
        finally
        {
           document.write("Finally Block entered: Welcome")
        }

      </
script>
   <
/head>

   <
body>
   </
body>
</
html>

The output of the above program is

‘junkVariable’ is undefined
Finally Block entered: Welcome

In the above program, after the try block is executed, the catch block throws the error. The finally statement is then executed last, displaying the second message “Finally Block entered: Welcome”

Additional usage information about try…catch..finally and throw statements:

Nested try…catch Statements:

It is also possible to have nested try..catch statements or one try..catch statement placed inside another.

for example:


try{
      ………           
//Block of code
      ………           
//Block of code
      try
      {
         ………        
//Block of code
         ………        
//Block of code
      }
      catch(Err)
      {
         ……..         
//Block of code
         ……..         
//Block of code
      }
      ……..           
 //Block of code
      ……..           
 //Block of code
}
catch(err1)
{
     ……..           
    //Block of code
     ……..             
//Block of code
}

finally

{

     ……..                //Block of code
     ……..             
//Block of code
}

* It is also possible to catch the error placed in the inner try..catch statement, then use that error in the outer catch statement of the nested try..catch statement using the throw statement of JavaScript.

* Using the throw statement, it is possible for the programmer to throw errors at any point.

« « JavaScript Exception Handling – Part I
JavaScript Event Handler » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Ads

Free Training

RSSSubscribe 416 Followers
Ads
  • Popular
  • Recent
  • JavaScript Features

    April 24, 2007 - 0 Comment
  • JavaScript Confirm Box

    April 25, 2007 - 0 Comment
  • JavaScript Location Object

    July 4, 2007 - 0 Comment
  • JavaScript Array Operations

    July 29, 2007 - 0 Comment
  • JavaScript Form Object

    May 19, 2007 - 0 Comment
  • Working with JavaScript DOM Objects

    June 4, 2007 - 0 Comment
  • JavaScript Objects

    August 12, 2007 - 0 Comment
  • JavaScript Window Object Timeout Methods

    August 8, 2007 - 0 Comment
  • JavaScript Events Handling

    May 20, 2007 - 0 Comment
  • JavaScript Frame object

    May 20, 2007 - 0 Comment
  • JavaScript Objects

    August 12, 2007 - 0 Comment
  • JavaScript String Object

    August 12, 2007 - 0 Comment
  • JavaScript Date Object

    August 12, 2007 - 0 Comment
  • JavaScript Math Object

    August 9, 2007 - 0 Comment
  • JavaScript Windows Object Properties Part II

    August 9, 2007 - 0 Comment
  • JavaScript Windows Object Properties Part I

    August 8, 2007 - 0 Comment
  • JavaScript Window Object Timeout Methods

    August 8, 2007 - 0 Comment
  • JavaScript Document Object Methods Part II

    August 7, 2007 - 0 Comment
  • JavaScript Document Object Methods Part I

    August 5, 2007 - 0 Comment
  • JavaScript History Object Properties and Methods

    August 5, 2007 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • JavaScript Objects
  • JavaScript String Object
  • JavaScript Date Object
  • JavaScript Math Object
  • JavaScript Windows Object Properties Part II

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2021. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled