Skip to content
Developerhome

Try/Catch/Finally

  Less than to read

The try/catch/finally structure is a fundamental part of error handling in Web Amendability. It allows you to handle errors and exceptions that may occur during the execution of your code. By utilising try/catch/finally blocks, you can implement custom error handling logic and provide a smooth user experience by displaying relevant success or error messages.

  • Try Block: The try block is used to enclose the code that might throw an error or exception. It is where you place the code that needs to be executed and monitored for any potential errors. If an error occurs within the try block, it is immediately caught by the catch block, preventing it from disrupting the normal flow of the application.

  • Catch Block: The catch block is executed when an error or exception is thrown within the try block. It allows you to define specific error-handling logic to handle the caught error. In the Web Amendability, catch blocks are often used to display error messages to the user or perform additional actions based on the type of error encountered. It provides you with an opportunity to handle errors and guide users through the error resolution process.

  • Finally Block: The finally block is optional and is executed regardless of whether an error occurred or not. It is useful for performing cleanup tasks or releasing resources that need to be executed regardless of the outcome.

"tryCatch": {
  "noThrow": [],
  "try": [
    {
      "apiPut": {
        "endpoint": "v1/departments",
        "prefix": "",
        "id": {
          "get": {
            "state": "dept_id"
          }
        }
      }
    },
    {
      "set": {
        "success_message": "Department updated successfully"
      }
    },
    {
      "resetState": {}
    }
  ],
  "catch": [
    {
      "set": {
        "error_message": {
          "title": "Error updating Department",
          "message": {
            "get": {
              "state": "CURRENT_ERROR"
            }
          }
        }
      }
    }
  ],
  "finally": [
    {
      "set": {
        "properties" : {
          "newTab1": {
            "visible":true
          }
        }
      }
    }
  ]
}

In this example, the try block contains an “apiPut” operation to update a department. If an error occurs during the API request, the catch block is executed. It sets the “error_message” state, which includes a title and a message retrieved from the “CURRENT_ERROR” state. This allows you to display a meaningful error message to the user, notifying them of the error that occurred.

We recommend viewing the following sections for more information: