JeeWiz Home  
The Model-Driven System Builder

JeeWiz Architect's Guide
 
Contents  >   8.  Templates and Velocity Features
 


8.8 #try

The #try statement allows you to catch Java exceptions. This can be useful when using Java objects that return an exception to indicate failure. If you do not catch the exception, it will cause the build job to be terminated. For example, here is some adapted code that shows this technique
#method( getIsStringValidAsInteger $stringParameter )
    #try
        #set( $dummy = $this.getClassInstance( "java.lang.Integer", "$stringParameter" ) )
        #return( true )
    #end
    #return( false )
#end
The method returns true if the String input parameter is valid as an Integer. It does this by calling the JeeWiz class constructor, for java.lang.Integer, with one String parameter. This throws an exception if the value is invalid.

The syntax is as follows:
  1. Option 1 - catching the exception:
    #try( acceptor )
        statements+
    #end
    
  2. Option 2 - no acceptor, - and the '()' is normally omitted too:
    #try
        statements+
    #end
    
In option 1, the acceptor is something that can accept a value and so be valid on the LHS of a #set() statement. $e is a good acceptor to use!

If option 1 is used, then the acceptor will be removed before the statements are executed. This means that a sequence like the following also makes sense:
#try( $e )
    #set( $dummy = $this.getClassInstance( "java.lang.Integer", "$stringParameter" ) )
#end
#if( $e )
    ...
#end
If there is an exception, then the $e variable will be set when the $dummy assignment is attempted, and the statements in the #if will be executed.

If there is no exception, then $e is guaranteed to be undefined and so the #if test will fail.

 


Copyright (c) 2001-2008 New Technology/enterprise Ltd.