Error Handling (try, catch, throw) in JavaScript
In JavaScript, you can handle errors using the try, catch, and finally blocks.
Syntax:
You can handle errors with the following syntax:
Syntax:
You can handle errors with the following syntax:
- Try block: This is where you write code that might throw an exception.
- Catch block: This block handles the error if one occurs in the try block.
- Finally block: This block always executes, regardless of whether an exception occurred or not.
Examples:
Basic try-catch example:
try { let numero = 10; console.log(numero.toUpperCase()); // This will throw an error } catch (error) { console.log("An error occurred:", error.message); }
Explanation:
In this example, the try block contains code that throws an error because you can't apply the toUpperCase method to a number. The catch block handles the error by printing a message to the console.
Example with finally:
try { let valor = JSON.parse("{invalidJson}"); } catch (error) { console.log("Error parsing JSON:", error.message); } finally { console.log("This always executes, no matter what happens."); }
Explanation:
Here, the try block attempts to parse invalid JSON, which causes an error. The catch block handles the error, while the finally block prints a message that will execute regardless of whether an error occurred.
Advanced example: controlled flow
function dividir(a, b) { try { if (b === 0) { throw new Error("Cannot divide by zero"); } return a / b; } catch (error) { console.log("Error:", error.message); return null; } finally { console.log("Operation finished"); } } console.log(dividir(10, 2)); // Output: 5 console.log(dividir(10, 0)); // Output: Error: Cannot divide by zero
Explanation:
In this example, the dividir function checks if the divisor is zero. If so, it throws a custom error withthrow. The catch block handles this error by returning null, while finally prints a message regardless of the result.
Purpose:
Error handling is useful for preventing your code from stopping unexpectedly and provides a way to deal with problems in a controlled manner.
Exercises
Interactive Quiz 1: Drag and Drop.
Arrastra en el orden correspondiente.
Arrastra las opciones:
Completa el código:
Interactive Quiz 2: Fill in the Blanks
Rellena los huecos en cada casilla.
function riskyFunction() { try { let result = someFunction(); } (error) { console.error("Error:", error); } { console.log("Operation completed."); } }
Exercise 3:
Complete the code in the editor to handle the error correctly.
JavaScript Concepts and Reference
Functions
Objects in JavaScript
Arrays
DOM (Document Object Model)
Error Handling
Promises and Asynchronicity
Modules in JavaScript
ES6 and Advanced Features
How many types of inheritance are there in JavaScript? What is inheritance in JavaScript? What are constructors in JavaScript? What are classes and inheritance? JavaScript classes examples JavaScript inheritance Classes in JavaScript JavaScript class methods JavaScript constructor Classes in JavaScript OOP Mozilla javascript class in javascript can a variable be declared without giving it a value.