dominoGuru.com

Your Development & Design Resource

Using Try, Catch, Finally, Throws, and Return Objects in Client Side and Server Side JavaScript for XPages

The try, catch, finally syntax is both something that I use every day, and something that I think most Notes Developers may not be familiar with when they make the transition to Web Developers. For those of you unfamiliar with the syntax, here's a brief breakdown via code example:

A simple syntax that's very readable, which is one of the reasons that I like it.

Before we move onto more advanced uses, let's discuss the throw statement... and we'll start with another code example:

In this case, if someValue is "", then it will display an alert with the "Error: someValue is BLANK!" message. Once that alert is dismissed, the code will continue to run (completing the try), and then alert with the "someValue: " + someValue.

Now that we have the basics down, let's take a look at a function that I might use on the beforeRenderResponse of one of my "Form"-type XPages:

You'll notice that I placed a variable named rBoolean and set it to false before my try, catch, finally block. This allows me to create a someForm_beforeRenderResponse() function success/failure state, that I can check as part of my application flow. For example, if I wanted to redirect to an error page if there was an issue in my someForm_beforeRenderResponse() function:

Taking this technique to the next level, you could establish a standard return object, which could sit outside of your try, catch, finally block and be functionally accessible via the calling function, event, or operation:

Now we can access the return object to get the returnBoolean, returnMessage, and the returnValue!

You might also notice, inside of the catch block, that I'm writing the thrown message to the facesContext. Yes, this is actually an SSJS function. Additionally, I'm not throwing when the isNan returns true (ie,. someNumber is actually not a number), as I wanted to show you alternative approaches without having to write another example function.

Conclusion

The try, catch, finally syntax gives us greater control over error handling with our JavaScript, and can be used in both ClientSide (ie,. "regular") JavaScript as well as IBM Notes Domino XPages ServerSide JavaScript (SSJS). Combine this with a standard return object that gives you a success/fail Boolean, a descriptive text message, and a return value (which can be any type of element you'd like, from String to a Boolean, to an Array to even a JSON Object), and the possibilities are limitless.

This approach additionally allows you to better craft your user experiences, since you can easily throw "Errors" when something doesn't match up with the business logic or workflow.

Finally (get it?!), this approach is not meant to replace UI validation, but it can -- again -- be used to enhance the user experience (I don't want there to be any confusion): the try, catch, finally syntax combined with the occasional throw in combination with a return object is something that you can use in your various UI and backend events to create a potentially more engaging and informative user experience.


About the author: Chris Toohey

Thought Leadership, Web & Mobile Application Development, Solutions Integration, Technical Writing & Mentoring

A published developer and webmaster of dominoGuru.com, Chris Toohey specializes in platform application development, solutions integration, and evangelism of platform capabilities and best practices.



More from dominoGuru.com

dominoGuru.com is powered by IBM Notes Domino XPages & hosted by Prominic.NET

Contact Us

Use our Contact / Feedback form or one of these email addresses:

Creative Commons License

Except where otherwise noted, dominoGuru.com by Chris Toohey is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.