feat(curriculum): Add interactive examples to try…catch…finally lesson (#63552)

This commit is contained in:
ishaanpgujaran
2025-11-06 02:42:06 +05:30
committed by GitHub
parent cecb373f0e
commit 8a5dc7d28f

View File

@@ -5,7 +5,7 @@ challengeType: 19
dashedName: how-does-try-catch-finally-work
---
# --description--
# --interactive--
In the previous lesson, you learned how to throw exceptions in your programs. In this lesson, we will take a look at how to gracefully handle these errors in a `try…catch…finally` block.
@@ -17,6 +17,8 @@ The `finally` block runs after the `try` and `catch` blocks, regardless of wheth
Here is an example of using a `try…catch` block:
:::interactive_editor
```js
function processInput(input) {
if (typeof input !== "string") {
@@ -35,6 +37,8 @@ try {
}
```
:::
In this example, we have a function called `processInput` that first checks if the `input` is not of type `string`. If that is the case, then we throw an error. Otherwise, we return the result of using the `toUpperCase` method on the `input`.
We call the function inside of a `try` block. Since the function call throws an error, then it will be caught inside the `catch` block and the error message will be displayed in the console.