Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

How to Use a JavaScript try-catch Block to Debug

When executing JavaScript, an error can occur. An error might be caused by a coding error, user error, or some other cause. The try-catch construct is a way to determine the cause of an error. Shown below is the basic code for a try-catch construct.

try
{
   // some code
}
catch(e)
{
   // code to run if try block fails
}

Within the try block is the code to be tested for errors while it is being executed. If an error occurs in the code within the try block, the catch method receives the error object. The error object has two properties, the number and the description. Shown below is code that might be placed within the catch block.

try  
{  
   // some code
}  
catch(e)  
{  
   document.write ("Number: ");  
   document.write (e.number);  
   document.write (" Description: ");  
   document.write (e.description);  
}  

If you are not sure where an error is coming from, you can use the reverse-binary trouble shooting method. Start by placing a large block of code in the try section. Then place a smaller block of code within the try section (about half at a convenient point in the code). Keep placing progressively smaller blocks of code (about half at a convenient point in the code) within the try section, until you've isolated the error down to the problem line.

More Java Script Code:
• Window Object Properties and Methods
• Define Lines in a String
• Format a Number as Currency
• Java Script Trigonometric Methods
• Java Script Events
• The Browsers History Object
• Extract Part of a String
• JavaScript to Concatenate Strings
• Java Script Data Types
• Convert a Number to a String

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268