Recall & Review
beginner
What is the purpose of the
try block in Jenkins pipelines?The
try block contains the code that Jenkins attempts to run. If an error occurs inside this block, Jenkins looks for a matching catch block to handle the error.Click to reveal answer
beginner
What does the
catch block do in a Jenkins pipeline?The
catch block runs only if an error happens inside the try block. It lets you handle errors gracefully, like sending notifications or cleaning up resources.Click to reveal answer
beginner
Explain the role of the
finally block in Jenkins pipelines.The
finally block runs no matter what — whether the try block succeeds or an error occurs. It is useful for cleanup tasks that must always happen, like deleting temporary files.Click to reveal answer
intermediate
Show a simple Jenkins pipeline snippet using
try-catch-finally.Example:
try {
echo 'Running main steps'
sh 'exit 1' // Simulate failure
} catch (err) {
echo "Caught error: ${err}"
} finally {
echo 'Always runs'
}Click to reveal answer
beginner
Why is using
try-catch-finally important in Jenkins pipelines?It helps keep your pipeline stable by handling errors without stopping the entire process unexpectedly. It also ensures important cleanup or notifications happen regardless of success or failure.
Click to reveal answer
In Jenkins pipelines, which block always runs regardless of errors?
✗ Incorrect
The finally block always runs whether the try block succeeds or an error occurs.
What happens if an error occurs inside the try block and there is no catch block?
✗ Incorrect
If no catch block handles the error, the finally block still runs, then the pipeline fails.
Which block is used to handle errors in Jenkins pipelines?
✗ Incorrect
The catch block is designed to handle errors thrown in the try block.
What is a common use of the finally block?
✗ Incorrect
The finally block is used to clean up resources or perform tasks that must always run.
How do you catch an error in a Jenkins scripted pipeline?
✗ Incorrect
try-catch blocks are used to catch and handle errors in scripted Jenkins pipelines.
Describe how try-catch-finally works in Jenkins pipelines and why it is useful.
Think about what happens when something goes wrong and what must always happen.
You got /4 concepts.
Write a simple Jenkins pipeline snippet using try, catch, and finally blocks to handle a failing shell command.
Use echo statements to show each block running.
You got /3 concepts.