Move the final closeSession() into a final try/catch statement

Drop the throw(err) approach as it does not work with command-line script
calling. Instead fill the value of the return code so that the examples can be
called as follows:
matlab -nodisplay -nosplash -nodesktop -r "script; exit(rc)"