How can I prevent one matrix cell with multiple stages from failing the whole job?

Jenkins setup:

Jenkins: 2.426.3
OS: Linux - 4.18.0-193.1.2.el8_2.x86_64
Java: 11.0.13 - Red Hat, Inc. (OpenJDK 64-Bit Server VM)

I’d like one cell (with multiple stages) in a matrix to be special and not cause the main job to fail if a stage within the cell fails. There’s information about using try/catch blocks and catchError options to do this for a single stage but not for multiple stages. Apparently the try/catch block is a script and can only contain steps so after a lot of message around I ended up with this structure:

        stage("Matrix build") {
          matrix {
            axes {
              axis {
                name "BUILD_VERSION"
                values "a", "b", "c", "d"
              }
            }
            stages {
              stage("Exception catcher") {
                steps {
                  script {
                    try {
                      stage("Primary matrix stage") {
                        stages {
                          stage("A") {
                            steps {
                              sh """
                                echo Hello
                              """
                            }
                          }

                          stage("B") {
                            when {
                              expression {
                                env.some_condition == "true"
                              }
                            }
                            steps {
                              sh """
                                echo Hello
                              """
                            }
                          }
                        }
                      }
                    }
                    catch (e) {
                      if (env.BUILD_VERSION == "c") {
                        echo "Ignoring exception in matrix cell for config `${env.BUILD_VERSION}': ${e.message}"
                      } else {
                        throw e
                      }
                    }
                  }
                }

However, I get an error message like so, which appears to be from the stages inside the try block:

2025-03-31 14:36:00 |  java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, ...

Is it even possible to cover multiple stages with a try/catch block? If so, what is the correct syntax?

Thanks.

You can’t put stages inside stage , but stage is also a normal step that you can use
The following should work I think (untested)

stage("Matrix build") {
  matrix {
    axes {
      axis {
        name "BUILD_VERSION"
        values "a", "b", "c", "d"
      }
    }
    stages {
      stage("Primary matrix stage") {
        steps {
          script {
            try {
              stage("A") {
                sh """
                  echo Hello
                """
              }

              stage("B") {
                sh """
                  echo Hello
                """
              }
            }
            catch (e) {
              if (env.BUILD_VERSION == "c") {
                echo "Ignoring exception in matrix cell for config `${env.BUILD_VERSION}': ${e.message}"
              } else {
                throw e
              }
            }
          }
        }
      }
    }
  }
}

Umm..

stages


| Allowed | Inside the pipeline block, or within a stage. |

Pipeline Syntax

No, I tried this already and then when options inside my stages (edit: edited example to show them) cause:

2025-03-31 16:36:15 |  java.lang.NoSuchMethodError: No such DSL method 'when' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, ...

The moment you enter the script block you’re no longer using declarative pipeline syntax but scripted syntax. And stages and when are not known steps in scripted pipeline.

So how do I do what I need?

You can use parallel in scripted instead.

Example: docker-agent/Jenkinsfile at ce8fc915c8b6a3787f487377e62fa22262c92017 · jenkinsci/docker-agent · GitHub
All these parallel stages are independants.

Instead of a matrix?

Yes, parallel instead of matrix