title: Jenkins Pipeline: Try/catch inside a retry block
body: <p>I have problems with my Jenkins pipeline. I want to do the following: 
  if the Build Environment already exists in the Workspace then execute a (incremental) build using the previous Environment.</p>
  
  <p>If that fails retry again with a clean build (delete previous Build Environment, then build again)</p>
  
  <p>I am trying to do this by:</p>
  
  <pre><code>retry(1) {
          try {
              prepareEnvironment()
              setupBuildEnvironment() // sets up environment if it is not present yet
              runBuild()
          } catch (e) {
              echo 'Err: Incremental Build failed with Error: ' + e.toString()
              echo '     Trying to build with a clean Workspace'
              removeOldBuildEnvironment()
          } finally {
              cleanupEnvironment()
          }
      }
  </code></pre>
  
  <p>When I run this in Jenkins and the <code>runBuild()</code> step fails I get:</p>
  
  <blockquote>
    <p>org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
    Scripts not permitted to use new java.lang.Exception java.lang.String</p>
  </blockquote>
  
  <p>I think the problem is the try/catch inside the retry(1) block. Any suggestions how to fix that</p>
  
  <p>I also tried it without the retry. Here I had the problem that if the </p>
  
  <pre><code>bat "..." 
  </code></pre>
  
  <p>step inside runBuild() returns a exception the pipeline Stage is marked as fail even if I catch the exception and the clean build is a success.</p>
  
  <p>Any suggestions?</p>