Bird
Raised Fist0
Jenkinsdevops~20 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Identify the main purpose in Jenkins pipelines

    This structure helps manage errors gracefully and ensures cleanup steps always run, making pipelines reliable.
  3. Final Answer:

    To handle errors and ensure cleanup actions run -> Option C
  4. 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 { ... }
B. try { ... } catch(Exception e) { ... } finally { ... }
C. try { ... } catch { Exception e } finally { ... }
D. try: { ... } except(Exception e) { ... } finally { ... }

Solution

  1. Step 1: Recall Jenkins scripted pipeline syntax

    Jenkins scripted pipelines use Groovy syntax, where try { ... } catch(Exception e) { ... } finally { ... } is correct.
  2. Step 2: Identify syntax errors in other options

    Options A, B, and C have incorrect keywords or misplaced braces not valid in Groovy.
  3. Final Answer:

    try { ... } catch(Exception e) { ... } finally { ... } -> Option B
  4. Quick Check:

    Groovy try-catch syntax = D [OK]
Hint: Groovy uses catch(Exception e) with braces, not colons or misplaced braces [OK]
Common Mistakes:
  • Using Python-style except instead of catch
  • Misplacing braces around catch block
  • Omitting parentheses around exception
3. What will be the output of this Jenkins scripted pipeline snippet?
try {
  echo 'Start'
  error('Fail here')
  echo 'End'
} catch (Exception e) {
  echo 'Caught error'
} finally {
  echo 'Cleanup'
}
medium
A. Start\nCaught error\nCleanup
B. Caught error\nCleanup
C. Start\nEnd\nCleanup
D. Start\nCleanup

Solution

  1. Step 1: Trace the try block execution

    The pipeline prints 'Start', then calls error('Fail here') which throws an exception, so 'End' is skipped.
  2. Step 2: Understand catch and finally blocks

    The catch block runs and prints 'Caught error'. The finally block always runs and prints 'Cleanup'.
  3. Final Answer:

    Start Caught error Cleanup -> Option A
  4. Quick Check:

    Exception skips 'End', catch and finally run = C [OK]
Hint: Remember: error stops try, catch runs, finally always runs [OK]
Common Mistakes:
  • Assuming code after error() runs
  • Thinking finally runs before catch
  • Ignoring that catch handles the error
4. Identify the error in this Jenkins pipeline snippet:
try {
  echo 'Running'
} catch {
  echo 'Error caught'
} finally {
  echo 'Done'
}
medium
A. try block must have a return statement
B. finally block cannot be used without catch
C. echo statements must be inside script block
D. Missing exception variable in catch block

Solution

  1. Step 1: Check catch block syntax

    In Groovy/Jenkins, catch must declare an exception variable like catch (Exception e). Here it is missing.
  2. Step 2: Verify other parts

    finally block is valid with catch, echo works outside script block in scripted pipeline, and try block does not require return.
  3. Final Answer:

    Missing exception variable in catch block -> Option D
  4. Quick Check:

    catch needs (Exception e) = A [OK]
Hint: Always declare exception variable in catch (e.g., catch(Exception e)) [OK]
Common Mistakes:
  • Omitting exception variable in catch
  • Thinking finally requires catch to be absent
  • Assuming echo needs script block
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

  1. 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.
  2. 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.
  3. Final Answer:

    Put cleanup code inside the finally block -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard