Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen The try and catch Blocks | Understanding and Handling Errors
Error Handling in JavaScript

bookThe try and catch Blocks

The try and catch blocks are fundamental tools in JavaScript for handling errors that occur during the execution of synchronous code. A try block lets you wrap code that might throw an error. If an error is thrown inside the try block, JavaScript immediately stops executing the code inside it and jumps to the catch block. The catch block receives the error object, allowing you to respond gracefully—such as logging the issue, displaying a message to the user, or taking corrective action. If no error occurs, the catch block is skipped entirely. This structure allows you to isolate risky operations and prevent your entire program from crashing due to unexpected problems.

12345678
const userInput = "{ name: 'Alice' }"; // Invalid JSON (single quotes instead of double quotes) try { const data = JSON.parse(userInput); console.log("Parsed data:", data); } catch (error) { console.log("Failed to parse JSON:", error.message); }
copy

In this example, you see how try and catch work together to manage a potential error. The userInput string is not valid JSON because it uses single quotes instead of double quotes. When the JSON.parse function tries to parse this string inside the try block, it throws a SyntaxError. JavaScript immediately stops executing the rest of the try block and moves to the catch block. The catch block receives the error object, which contains details about what went wrong. Here, the code logs a clear message along with the error's message property, helping you understand the issue without stopping the rest of your program. If the input had been valid JSON, the catch block would have been skipped, and you would see the parsed data logged instead. This pattern is essential for writing robust code that can handle unexpected situations gracefully.

question mark

What is the main purpose of using try and catch blocks in JavaScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 7.69

bookThe try and catch Blocks

Swipe um das Menü anzuzeigen

The try and catch blocks are fundamental tools in JavaScript for handling errors that occur during the execution of synchronous code. A try block lets you wrap code that might throw an error. If an error is thrown inside the try block, JavaScript immediately stops executing the code inside it and jumps to the catch block. The catch block receives the error object, allowing you to respond gracefully—such as logging the issue, displaying a message to the user, or taking corrective action. If no error occurs, the catch block is skipped entirely. This structure allows you to isolate risky operations and prevent your entire program from crashing due to unexpected problems.

12345678
const userInput = "{ name: 'Alice' }"; // Invalid JSON (single quotes instead of double quotes) try { const data = JSON.parse(userInput); console.log("Parsed data:", data); } catch (error) { console.log("Failed to parse JSON:", error.message); }
copy

In this example, you see how try and catch work together to manage a potential error. The userInput string is not valid JSON because it uses single quotes instead of double quotes. When the JSON.parse function tries to parse this string inside the try block, it throws a SyntaxError. JavaScript immediately stops executing the rest of the try block and moves to the catch block. The catch block receives the error object, which contains details about what went wrong. Here, the code logs a clear message along with the error's message property, helping you understand the issue without stopping the rest of your program. If the input had been valid JSON, the catch block would have been skipped, and you would see the parsed data logged instead. This pattern is essential for writing robust code that can handle unexpected situations gracefully.

question mark

What is the main purpose of using try and catch blocks in JavaScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
some-alt