0
0
Jenkinsdevops~20 mins

Try-catch-finally in pipelines in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Try-Catch-Finally Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline snippet?

Consider this Jenkins scripted pipeline snippet using try-catch-finally:

node {
  try {
    echo 'Start build'
    error 'Build failed'
  } catch (Exception e) {
    echo "Caught error: ${e.message}"
  } finally {
    echo 'Cleanup actions'
  }
}

What will Jenkins print in the console?

Jenkins
node {
  try {
    echo 'Start build'
    error 'Build failed'
  } catch (Exception e) {
    echo "Caught error: ${e.message}"
  } finally {
    echo 'Cleanup actions'
  }
}
A
Start build
Caught error: Build failed
Cleanup actions
B
Start build
Build failed
Cleanup actions
C
Start build
Cleanup actions
D
Caught error: Build failed
Cleanup actions
Attempts:
2 left
💡 Hint

Remember that error throws an exception caught by catch, and finally always runs.

🧠 Conceptual
intermediate
1:30remaining
Which statement about try-catch-finally in Jenkins pipelines is true?

Choose the correct statement about the behavior of try-catch-finally blocks in Jenkins scripted pipelines.

AIf an error occurs in the finally block, it is ignored by Jenkins.
BThe finally block runs only if the try block succeeds without errors.
CThe finally block runs only if the catch block is executed.
DThe catch block can catch any exception thrown inside the try block.
Attempts:
2 left
💡 Hint

Think about how exceptions propagate and how finally always runs.

🔀 Workflow
advanced
2:30remaining
Order the steps Jenkins executes in this try-catch-finally pipeline

Given this Jenkins scripted pipeline snippet, order the steps as Jenkins executes them:

node {
  try {
    echo 'Step 1'
    error 'Fail here'
    echo 'Step 2'
  } catch (Exception e) {
    echo 'Step 3'
  } finally {
    echo 'Step 4'
  }
  echo 'Step 5'
}
A1,2,4,5,6,3
B1,2,3,4,5,6
C1,2,4,5,6
D1,4,2,5,6,3
Attempts:
2 left
💡 Hint

Remember that after error is called, the rest of try block is skipped.

Troubleshoot
advanced
2:00remaining
Why does this Jenkins pipeline fail without printing 'Cleanup'?

Look at this Jenkins scripted pipeline snippet:

node {
  try {
    echo 'Build started'
    sh 'exit 1'
  } catch (Exception e) {
    echo 'Caught error'
  } finally {
    sh 'exit 1'
    echo 'Cleanup'
  }
}

Why does Jenkins fail without printing 'Cleanup'?

AThe catch block does not catch shell command errors.
BThe shell command in finally fails and stops execution before echo 'Cleanup'.
CThe finally block runs before the catch block, so 'Cleanup' is skipped.
DJenkins ignores errors in finally and continues, so this should print 'Cleanup'.
Attempts:
2 left
💡 Hint

Consider the order of commands in finally and what happens if a shell command fails.

Best Practice
expert
3:00remaining
Which Jenkins pipeline pattern ensures cleanup always runs even if cleanup commands fail?

In Jenkins scripted pipelines, you want to ensure cleanup commands always run, and even if a cleanup command fails, the pipeline continues to run all cleanup steps. Which pattern achieves this?

AWrap each cleanup command inside its own try-catch inside the finally block.
BPut all cleanup commands inside finally block without try-catch inside finally.
CPut cleanup commands only in the catch block to run on errors.
DUse a separate post { always { ... } } block instead of try-catch-finally.
Attempts:
2 left
💡 Hint

Think about how to handle errors inside finally to avoid stopping cleanup early.