Your Development & Design Resource
08/26/2015 by Chris Toohey
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
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
returnMessage, and the
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.
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.