Home     |      Learn AJAX     |      Forum     |      Register     |      Submit Resource     |      Submit Article      |      Contact Us

Home JavaScript
 

Using Try/Catch Method in Ajax

 

Ajax based developers who opted not to use frameworks have the luxury of customizing their application to whatever they like. However, they also run the risk of building an application that may not work because the coding was not properly done. If this happens, developers have to work on their codes again.


Of course, there is a stage wherein developers will have to check the validity of their application, but if they are able to minimize errors right from the start, development is faster and correcting errors would be a lot easier.


When error in coding happens, users maybe able to access the webpage but will be prompted with a debug action. Oftentimes, users will ignore the prompt but ultimately abandon the webpage since they are unsure of the usability of the website. Eventually, the website will have less visitors because of the error in coding.


Developers could prevent the debug error in their websites by employing the try...catch technique. As this is applicable in JavaScript, this could be easily integrated in an Ajax based application. Ajax may have additional languages for better user experience such as XML, HTML, PHP and others but this wouldn't matter or the coding will not be affected by try...catch. This coding technique is placed before (try) the function or command and after (catch) the command:


try
{
//function
}
catch(error type)
{
//command when error happens
}


Basically, try....catch will test your webpage for errors and if the function finds errors, developers can create commands which will instruct the user on what to do, redirect to another webpage or anything the developer fancies. If there are no errors found, the command will go through smoothly. It's a simple trick which could be done in any function.


You may be wondering why this method is not really popular in Ajax and JavaScript websites if this trick is really useful. You can blame that fact partly to frameworks who generates the coding for non-JavaScript developers and you can also blame the fact that try...catch should be regulated in a website.


For example, developers should not just use try...catch in any function they meet. If every function would be used with this tool, users will have an interactive experience with the website. But try...catch will eat up a lot of bandwidth and the server's power since try....catch will be monitoring the functions while the function is also actively running. That doubles the bandwidth requirement of the Ajax based website which will slow down it's execution and eventually affect user experience.


That is why it is important to limit the use of try...catch. Developers have to know which part of their application which would likely yield an error. Smartly placing try...catch in functions will ensure that the website will provide the expected user experience or even more without sacrificing bandwidth.


A tip: NEVER write try...catch in uppercase since it will yield an error. The function will only work in IE5 or better and Mozilla 1.0 or better.



Read Next: Optimizing Event Handlers



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links