Unfortunately, for now, I think the only way is to use try catch in a script block and re-throw the error after performing the error handling. See example below:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    script {
                        try {
                            sh "exit 1"
                        } catch (e) {
                            echo 'send email'
                            throw e
                        }
                    }
                }
            }
        }
        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}
Answer from Erik B on Stack Overflow
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
Equivalent to catchError(message: message, buildResult: 'UNSTABLE', stageResult: 'UNSTABLE'). ... A message that will be logged to the console if an error is caught. The message will also be associated with the stage result and may be shown ...
Discussions

Jenkins: Ignore failure in pipeline build step - Stack Overflow
It names a few steps as examples, but not as the only possible steps to be used as options. Also, if you enter an invalid option, Jenkins lists all available options in the error message, which includes catchError. ... Save this answer. ... Show activity on this post. For my decalartive pipeline I ... More on stackoverflow.com
🌐 stackoverflow.com
Jenkins: Aborting a build with a catchError() - Stack Overflow
In my Jenkins scripted pipeline, I am executing 1 stage with some tasks and then sending Slack messages. I want to send these Slack messages if the previous stage passes or fails and to do this I am using a catchError() and it works exactly as I need to. More on stackoverflow.com
🌐 stackoverflow.com
check on github says ok while stage failed on jenkins (using catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') in Jenkinsfile)
check on github says ok while stage failed on jenkins (using catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') in Jenkinsfile)#47 More on github.com
🌐 github.com
2
August 12, 2019
groovy - Try-catch block in Jenkins pipeline script - Stack Overflow
I'm trying to use the following code to execute builds, and in the end, execute post build actions when builds were successful. Still, I get a MultipleCompilationErrorsException, saying that my try... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/jenkinsci › what does the pipeline groovy script: catch(error) , error object consist of?
r/jenkinsci on Reddit: What does the pipeline groovy script: catch(error) , error object consist of?
March 23, 2021 -

and how would I print it or see it? For some reason any log line printing this error out in the groovy projects I am inheriting prints a dogshit stacktrace. Yes I used the technical term. At work of course the plugin has to be in action to be used so there is no stepping through like I am used to coming from a java dev background. So is there a doc, online resource or maybe simple tutorial I could go through on my own to simulate this and play around to better my understanding?

pseudocode of the instance I mean just to be clear:

try{

//some code that fails

}catch(error){

logger.error(error);

}

Edit: clearing up my gripe: the logged error will have the line number for the logger.error() line and not the line the error occurred on?

Top answer
1 of 12
116

To ignore a failed step in declarative pipeline you basically have two options:

  1. Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)
stage('someStage') {
    steps {
        script {
            try {
                build job: 'system-check-flow'
            } catch (err) {
                echo err.getMessage()
            }
        }
        echo currentBuild.result
    }
}
  1. Use catchError
stage('someStage') {
    steps {
        catchError {
            build job: 'system-check-flow'
        }
        echo currentBuild.result
    }
}

In both cases the build won't be aborted upon exception in build job: 'system-check-flow'. In both cases the echo step (and any other following) will be executed.

But there's one important difference between these two options. In first case if the try section raises an exception the overall build status won't be changed (so echo currentBuild.result => SUCCESS). In the second case you overall build will fail (so echo currentBuild.result => FAILURE).

This is important, because you can always fail the overall build in first case (by setting currentBuild.result = 'FAILURE') but you can't repair build in second option (currentBuild.result = 'SUCCESS' won't work).

2 of 12
103

In addition to simply making the stage pass, it is now also possible to fail the stage, but continue the pipeline and pass the build:

pipeline {
    agent any
    stages {
        stage('1') {
            steps {
                sh 'exit 0'
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    sh "exit 1"
                }
            }
        }
        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:

As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.

Just make sure your Jenkins is up to date, since this feature is only available since "Pipeline: Basic Steps" 2.16 (May 14, 2019). Before that, catchError is still available but without parameters:

        steps {
            catchError {
                sh "exit 1"
            }
        }
🌐
KodeKloud Notes
notes.kodekloud.com › docs › Certified-Jenkins-Engineer › Code-Quality-and-Testing › Demo-Code-Coverage-and-Catch-Errors › page
Demo Code Coverage and Catch Errors - KodeKloud
ERROR: Coverage for lines (79.1%) ... instead of FAILED, allowing later stages to run. The catchError step lets you control both the stage and build result. For full syntax, see the Jenkins Pipeline Syntax reference....
🌐
Kubedemy
kubedemy.io › home › jenkins tutorial – part 9 – basic pipeline steps
Jenkins Tutorial - Part 9 - Basic Pipeline Steps | Kubedemy
February 12, 2024 - catchError catches the error and continues the pipeline. The pipeline’s default behaviour is to fail the whole pipeline if any error occurs during each step.
🌐
Baeldung
baeldung.com › home › jenkins › set stage status in jenkins pipelines
Set Stage Status in Jenkins Pipelines | Baeldung on Ops
November 23, 2024 - Here, we use the catchError step to execute a command, and if the command fails, we’ll set the stage and build status to SUCCESS. Let’s trigger our pipeline:
Find elsewhere
🌐
GitHub
gist.github.com › darinpope › 1dfd9b0fd71a808e73bde649b4d8abc5
What-is-catchError-in-Jenkins.md - Gist - GitHub
What-is-catchError-in-Jenkins.md · Gist for https://youtu.be/u-1lyoas3ys · catchError build step · pipeline { agent any stages { stage('Hello') { steps { sh 'exit 0' } } stage('stage2') { steps { sh "echo stage2" } } } } pipeline { agent any stages { stage('Hello') { steps { sh 'exit 1' } } stage('stage2') { steps { sh "echo stage2" } } } } pipeline { agent any stages { stage('Hello') { steps { catchError { sh 'exit 1' } } } stage('stage2') { steps { sh "echo stage2" } } } } pipeline { agent any stages { stage('Hello') { steps { catchError(message:'news') { sh 'exit 1' } } } stage('stage2') { steps { sh "echo stage2" } } } } pipeline { agent any stages { stage('Hello') { steps { catchError(message:'news',buildResult:'UNSTABLE',stageResult:'UNSTABLE') { sh 'exit 1' } } } stage('stage2') { steps { sh "echo stage2" } } } } Sign up for free to join this conversation on GitHub.
🌐
Jenkins-ci
wiki.jenkins-ci.org › JENKINS › Pipeline-Basic-Steps-Plugin.html
Jenkins : Pipeline Basic Steps Plugin
September 7, 2021 - Equivalent to catchError(message: message, buildResult: 'UNSTABLE', stageResult: 'UNSTABLE')
🌐
DevOpsSchool.com
devopsschool.com › home › jenkins tutorials: scripted pipeline jenkinsfile example
Jenkins Tutorials: Scripted Pipeline jenkinsfile example - DevOpsSchool.com
December 23, 2022 - Pipeline: Basic Steps catchError: Catch error and set build result to failuredeleteDir: Recursively delete the current directory from the workspacedir: Change
🌐
DeepWiki
deepwiki.com › jenkinsci › workflow-basic-steps-plugin › 4.1-catcherror-step
CatchError Step | jenkinsci/workflow-basic-steps-plugin | DeepWiki
November 4, 2025 - The catchError step provides fault-tolerant error handling in Jenkins Pipeline by catching exceptions within a block, logging them, setting build and stage results, and allowing pipeline execution to continue.
🌐
Shenxianpeng
shenxianpeng.github.io › 2023 › 12 › jenkins-catch-error
如何让 Jenkins Pipeline 在特定错误发生时不中断失败
December 15, 2023 - 有时候,我们并不希望 ...失败。 这时可以使用 catchError 来捕获错误,并将阶段(stage)或构建(build)结果更新为 SUCCESS、UNSTABLE 或 FAILURE(如果需要的话)。...
🌐
Szymon Stepniak
e.printstacktrace.blog › home › jenkins pipeline cookbook › how to time out jenkins pipeline stage and keep the pipeline running?
How to time out Jenkins Pipeline stage and keep the pipeline running?
April 16, 2020 - It hides the information about the timeout, so we need to click on the stage and check if there was a timeout or not every time we run the pipeline. There should be a better way to handle it. Luckily, there is a better solution. We can combine both catchError with the try-catch. Let’s take a look at the final example. Listing 4. Jenkinsfile
🌐
YouTube
youtube.com › watch
What is catchError in Jenkins?
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
GitHub
github.com › jenkinsci › github-autostatus-plugin › issues › 47
check on github says ok while stage failed on jenkins (using catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') in Jenkinsfile) · Issue #47 · jenkinsci/github-autostatus-plugin
August 12, 2019 - With catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#catcherror-catch-error-and-set-build-result-to-failure I can set the stage to fail while it doesn't break the whole build.
Author   jenkinsci
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-57826
Loading...
February 24, 2020 - Jenkins: 2.164.3 Pipeline: Basic Steps: 2.16 · If I have a stage with a step in it: ... catchError(buildResult: hudson.model.Result.SUCCESS, message: 'RPM build failed, but allowing job to continue', stageResult: hudson.model.Result.FAILURE) { sh label: env.STAGE_NAME, script: 'exit 2' } With a post block for the stage: post { always { archiveArtifacts artifacts: 'artifacts/**' } success { println "${env.STAGE_NAME}: SUCCESS" } unstable { println "${env.STAGE_NAME}: UNSTABLE" } failure { println "${env.STAGE_NAME}: FAILED" } } I actually get the: My Test Stage: SUCCESS ·