📖 Scenario: You are creating a Jenkins pipeline script to automate a simple build process. Sometimes the build step might fail, and you want to handle errors gracefully. You also want to always run a cleanup step at the end, no matter if the build succeeded or failed.
🎯 Goal: Build a Jenkins pipeline script that uses try, catch, and finally blocks to run a build step, handle any errors by printing a message, and always run a cleanup step.
📋 What You'll Learn
Create a pipeline block with agent any
Inside stages, create a stage named Build
Use try block to run a shell command sh 'exit 1' to simulate failure
Use catch block to catch errors and print Build failed
Use finally block to print Cleaning up
💡 Why This Matters
🌍 Real World
In real Jenkins pipelines, builds can fail due to many reasons. Using try-catch-finally helps keep the pipeline stable and ensures cleanup steps always run.
💼 Career
Understanding error handling in Jenkins pipelines is essential for DevOps engineers to create reliable automation scripts.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Create a Jenkins pipeline with agent any and an empty stages block.
Jenkins
Hint
Start by writing pipeline { agent any stages { } to set up the pipeline skeleton.
2
Add a Build stage with a try block
Inside stages, add a stage named Build with a steps block. Inside steps, add a script block containing a try block that runs sh 'exit 1' to simulate a failure.
Jenkins
Hint
Use stage('Build') and inside steps add script { try { sh 'exit 1' } }.
3
Add catch and finally blocks
Inside the script block, after the try block, add a catch (err) block that prints Build failed using echo. Then add a finally block that prints Cleaning up using echo.
Jenkins
Hint
After try, add catch (err) { echo 'Build failed' } and finally { echo 'Cleaning up' }.
4
Print the final pipeline script output
Run the pipeline script and observe the output. The output should include Build failed and Cleaning up messages.
Jenkins
Hint
Look at the Jenkins console output after running the pipeline. You should see both messages printed.
Practice
(1/5)
1. What is the main purpose of using try-catch-finally blocks in Jenkins pipelines?
easy
A. To create parallel stages automatically
B. To speed up the pipeline execution
C. To handle errors and ensure cleanup actions run
D. To deploy applications to production
Solution
Step 1: Understand the role of try-catch-finally
The try block runs code that might fail, catch handles errors, and finally runs cleanup code regardless of success or failure.
Step 2: Identify the main purpose in Jenkins pipelines
This structure helps manage errors gracefully and ensures cleanup steps always run, making pipelines reliable.
Final Answer:
To handle errors and ensure cleanup actions run -> Option C
Quick Check:
Error handling and cleanup = A [OK]
Hint: Remember: try for risky code, catch errors, finally cleans up [OK]
Common Mistakes:
Thinking try-catch-finally speeds up pipelines
Confusing try-catch-finally with deployment steps
Assuming finally runs only on success
2. Which of the following is the correct syntax to catch an exception in a Jenkins scripted pipeline?
easy
A. try { ... } catch Exception e { ... } finally { ... }
5. You want to run a cleanup step in your Jenkins pipeline regardless of success or failure, but only if the try block ran without syntax errors. Which is the best way to structure your pipeline?
hard
A. Put cleanup code inside the finally block
B. Put cleanup code inside the catch block
C. Put cleanup code after the try-catch-finally block
D. Put cleanup code inside the try block after risky steps
Solution
Step 1: Understand when finally runs
The finally block runs after try and catch, regardless of success or failure, but only if the pipeline syntax is valid and the try block started.
Step 2: Compare options for cleanup placement
Cleanup in catch runs only on errors, after block runs only if no errors, and inside try risks skipping on errors. finally is best for guaranteed cleanup.
Final Answer:
Put cleanup code inside the finally block -> Option A
Quick Check:
Cleanup always runs in finally = B [OK]
Hint: Use finally for guaranteed cleanup after try-catch [OK]
Common Mistakes:
Putting cleanup only in catch (misses success case)
Placing cleanup after try-catch (may skip on errors)
Putting cleanup inside try (skipped if error thrown early)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.