Jenkins

Jenkinsfile Try-Catch

The try-and-catch blocks in Jenkins are a way to handle exceptions that may be thrown during the execution of a Jenkins pipeline. They allow us to define a code block and specify separate blocks to execute if an exception is thrown.

This can be useful for handling specific error conditions and taking appropriate actions, such as retrying a failed build or logging an error message.

Jenkins Try and Catch Blocks

To use the try-and-catch blocks in Jenkins, we can use a script block and define a Groovy code inside that block.

The following shows the syntax of the try-and-catch blocks we can use in Jenkins:

try {
   // code block to test
} catch (Exception e) {
   // execute if any exception is thrown
}

In the try block, we can put any code that we want to be executed. If an exception is thrown during the execution, the pipeline will jump to the catch block which defines  the code to be executed in case of an exception

We can also specify a specific type of exception to catch by setting it in the catch block. For example:

try {
   // code block to run
} catch (IOException e) {
   // execute if an IOException is thrown
}

This can be useful if we want to handle different types of exceptions differently. For example, we might want to retry a build if it fails due to a network error but log an error message and exit if it fails due to a syntax error.

How to Handle Multiple Exceptions

We can use multiple catch blocks to handle different types of exceptions. For example:

try {
   // code block to test
} catch (IOException e) {
   // execute if an IOException is thrown
} catch (NullPointerException e) {
   // execute if a NullPointerException is thrown
}

In this case, if an IOException is thrown, the code in the first catch block will be executed. If a NullPointerException is thrown, the code in the second catch block will be executed.

The Finally block

In addition, to try and catch blocks, we can also use a finally block to specify code that should be executed regardless of whether an exception is thrown.

For example, the following syntax shows the usage of the finally block in a Jenkins pipeline:

try {
   // code block
} catch (Exception e) {
   // execute on exception
} finally {
   // execute regardless of whether an exception is thrown
}

The code in the finally block will be executed after the try block and any catch blocks have been executed. This can be useful for cleaning up resources or performing other tasks that should be done regardless of whether an exception was thrown.

Example Usage

The following example pipeline demonstrates how we can use the try-and-catch blocks in a Jenkins pipeline:

pipeline {
  agent any

  stages {
      stage('Prompt for input') {
          steps {
              script {
                  def userInput
                  userInput = input message: 'Enter a value:', id: 'user_input'
                  try {
                      echo "User input: ${userInput}"
                  } catch (Exception e) {
                      userInput = input message: 'Invalid input. Please enter a valid value:', id: 'user_input'
                      retry(3)
                  }
              }
          }
      }
  }

}

In this declarative pipeline, the script block encloses a Groovy code that prompts the user for a given input. If no input is provided, the script will retry the prompt three times before aborting.

Conclusion

In this article, we learned how to use the try-and-catch blocks in the Jenkins pipeline to handle exceptions.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list