# tryCatch

## Description

Handling exceptions (errors) in code execution. It allows you to "try" a block of code, "catch" any errors that occur, and optionally "finally" execute some code regardless of whether an error was thrown or not

## Input / Parameter

N/A

## Output

N/A

## Callback

### try callback

The functions to be executed.

### catch callback

The functions to be executed when there is an exception.

| Description       | Output Type |
| ----------------- | ----------- |
| Returns an error. | Error       |

### finally callback

The functions to be executed regardless of the exception is thrown or caught.

## Example

In this example, we will do console logging by using `console` function in `tryCatch` function callback to demonstrate how `tryCatch` works.

### Code Equivalent

```
try {
  console.log("try")
} catch (e) {
  console.log(e)
} finally {
  console.log("finally");
}
```

### Steps

1. Drag a `button` component into the canvas and open the `Action` tab. Select the `press` event of the button and drag the `tryCatch` function to the event flow.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-d1a38697dfd8c357312a27b8d2351b3ebd1f6800%2FtryCatch-step-1.png?alt=media)
2. Drag the function `console` to the `tryCatch` function try callback, on it's `value` param add `try` as value.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-f3baedadaca36c7ea7f86428b96d57579c548956%2FtryCatch-step-2.png?alt=media)
3. Drag the function `console` to the `tryCatch` function catch callback, on it's `value` param change the param type to `input`.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-853c26483d7c855013cf239c827d4e3207fdded7%2FtryCatch-step-3.png?alt=media)
4. Drag the function `console` to the `tryCatch` function catch callback, on it's `value` param add `finally` as value.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-4ee60fafcf527115d9700a5dd096af8eddcd25cc%2FtryCatch-step-4.png?alt=media)

### Result

1. Now click the button in preview, it will only show `try` and `finally` on the console.

   ![](https://399701567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwmJ1RKjM2uNFaL6fO3Xu%2Fuploads%2Fgit-blob-1d8f6f4652c7a901ae6ab8a1cbe23268e0d84216%2FtryCatch-result-1.png?alt=media)

## Links

### Related Information

See also:

* Functions
  * [console](https://docs.emobiq.com/emobiq-client/006-actions-and-visual-logic/action-reference/cordova/app/console)
