0
0
Jenkinsdevops~5 mins

Try-catch-finally in pipelines in Jenkins - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acatch
Btry
Cfinally
Dnone
What happens if an error occurs inside the try block and there is no catch block?
Afinally block runs then pipeline fails
BPipeline fails immediately
CPipeline ignores the error
DPipeline retries automatically
Which block is used to handle errors in Jenkins pipelines?
Atry
Bcatch
Cfinally
Derror
What is a common use of the finally block?
AClean up resources regardless of success or failure
BSend notifications only on success
CRun main build steps
DSkip error handling
How do you catch an error in a Jenkins scripted pipeline?
AUsing <code>catchError</code> step
BUsing <code>error</code> step
CUsing <code>finally</code> block
DUsing <code>try-catch</code> blocks
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.