Answers: <p>You may use and update a flag to <code>false</code> in the <code>catch</code> and then use that flag in the <code>when</code> directive of other stage(s) that you want to skip because of any failure in the given stage. Example:  </p>
  
  <pre><code>boolean flagStageOneSuccess = false
  
  pipeline {
      agent any
  
      stages {
          stage ('stage-1') {
              steps {
                  try {
                      // Do something that might fail
  
                      flagStageOneSuccess = true
  
                  } catch (Exception e) {
                      // Handle the exception
                      flagStageOneSuccess = false
                  }
              }
          }
  
          stage ('stage-2') {
              when {
                  equals expected: true, actual: flagStageOneSuccess
              }
              steps {
                  // Do something in stage-2
              }
          }
      }
  }
  </code></pre>
  
  <p>It would also benefit you in viewing the status of your stage in <code>Blue Ocean</code>. If the stage is skipped, it shows as 'Not built'.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-05T05:31:38.983Z
Answers: 2019-12-05T05:41:04.413Z
Answers: 2019-12-05T05:41:04.413Z
Answers: Sid
Body: <p>I have a pipeline with some stages and logic. The only possible debugger is adding try-catch to each stage:</p>
  
  <pre><code>stage ("distribution"){
              steps{
                  script{
                      try{
      amd_distribution_distribute_bundle
                      }
                     catch (Exception e) {
                      echo e.toString()
                      }
              }
          }
      }
  </code></pre>
  
  <p>however, if one stage fails, I need to stop the pipeline:</p>
  
  <pre><code>status = sh script: '''
                  set +x
                  python3.4 main.py --command create_bundle
                  ''',  returnStatus:true
               }
          }
       }
  
      if (status == 1) {
      error("returning 1 from func")
      }
  </code></pre>
  
  <p>but the try-catch from the pipeline actually cancel it (it proceed to other stages. is there a way to combine them?</p>
  
Tags: <jenkins-pipeline><groovy>
Title: Is it possible to combine failure stage from groovy with try catch from pipeline?
UserDisplayName: arielma
ThreadText: # Is it possible to combine failure stage from groovy with try catch from pipeline?
  
  (Asked by: username_0 on 2019-11-26)
  
  I have a pipeline with some stages and logic. The only possible debugger is adding try-catch to each stage:
  
  ```
  stage ("distribution"){
              steps{
                  script{
                      try{
      amd_distribution_distribute_bundle
                      }
                     catch (Exception e) {
                      echo e.toString()
                      }
              }
          }
      }
  
  ```
  
  however, if one stage fails, I need to stop the pipeline:
  
  ```
  status = sh script: '''
                  set +x
                  python3.4 main.py --command create_bundle
                  ''',  returnStatus:true
               }
          }
       }
  
      if (status == 1) {
      error("returning 1 from func")
      }
  
  ```
  
  but the try-catch from the pipeline actually cancel it (it proceed to other stages. is there a way to combine them?
  
  **Tags:** jenkins-pipeline, groovy
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-05. Score: 4 | Accepted answer
  
  You may use and update a flag to `false` in the `catch` and then use that flag in the `when` directive of other stage(s) that you want to skip because of any failure in the given stage. Example:
  
  ```
  boolean flagStageOneSuccess = false
  
  pipeline {
      agent any
  
      stages {
          stage ('stage-1') {
              steps {
                  try {
                      // Do something that might fail
  
                      flagStageOneSuccess = true
  
                  } catch (Exception e) {
                      // Handle the exception
                      flagStageOneSuccess = false
                  }
              }
          }
  
          stage ('stage-2') {
              when {
                  equals expected: true, actual: flagStageOneSuccess
              }
              steps {
                  // Do something in stage-2
              }
          }
      }
  }
  
  ```
  
  It would also benefit you in viewing the status of your stage in `Blue Ocean`. If the stage is skipped, it shows as 'Not built'.
  
  
  ---
  
  