0
0
Jenkinsdevops~10 mins

Try-catch-finally in pipelines in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Try-catch-finally in pipelines
Start Pipeline
Try Block: Run Steps
Error Occurs?
NoSkip Catch
Yes
Catch Block: Handle Error
Finally Block: Always Runs
End Pipeline
The pipeline runs the try block steps first. If an error happens, it jumps to catch. Finally always runs at the end.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        script {
          try {
            sh 'exit 1'
          } catch (err) {
            echo "Caught error: ${err}"
          } finally {
            echo 'Always runs'
          }
        }
      }
    }
  }
}
This pipeline tries to run a failing shell command, catches the error, then runs the finally block.
Process Table
StepActionResultNext Step
1Start pipeline and enter try blockNo error yetRun shell command
2Run shell command 'exit 1'Command fails with error code 1Jump to catch block
3Catch block runsError caught and message printedRun finally block
4Finally block runsMessage 'Always runs' printedPipeline ends
💡 Pipeline ends after finally block runs
Status Tracker
VariableStartAfter Step 2After Step 3Final
errnullerror object from shell failureerror object retainederror object retained
Key Moments - 2 Insights
Why does the pipeline run the finally block even after an error?
The finally block always runs regardless of errors, as shown in execution_table step 4, ensuring cleanup or final steps happen.
What happens if no error occurs in the try block?
If no error occurs, the catch block is skipped and the pipeline goes directly to finally, as shown by the 'No error yet' in step 1 and skipping step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe shell command fails with error code 1
BThe shell command runs successfully
CThe catch block runs
DThe finally block runs
💡 Hint
Check the 'Result' column for step 2 in the execution_table
At which step does the pipeline print the error message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the 'Catch block runs' action in the execution_table
If the shell command succeeded, which step would be skipped?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Refer to key_moments about what happens if no error occurs
Concept Snapshot
Try-catch-finally in Jenkins pipelines:
- try: run steps that may fail
- catch: handle errors if try fails
- finally: always runs after try/catch
- ensures cleanup or messages run no matter what
- catch skipped if no error
- finally always runs
Full Transcript
In Jenkins pipelines, the try-catch-finally structure helps manage errors. The pipeline starts by running the try block steps. If an error occurs, it jumps to the catch block to handle it. Regardless of error or not, the finally block always runs at the end. This ensures important cleanup or messages always happen. For example, running a shell command that fails triggers the catch block to print the error, then the finally block runs. If the command succeeds, the catch block is skipped but finally still runs. This flow helps keep pipelines reliable and clear.