๐ Scenario: Imagine you are writing a program that tries to read a file and process its content. Sometimes the file might not exist, causing an error. You want to make sure that no matter what happens, a message is always shown to say the process is finished.
๐ฏ Goal: You will create a simple program that uses a try, catch, and finally block. The finally block will always run, whether an error happens or not.
๐ What You'll Learn
Create a variable called fileContent with the value "Hello, world!".
Create a variable called fileExists and set it to false.
Write a try block that throws an error if fileExists is false.
Write a catch block that catches the error and sets fileContent to "Default content".
Write a finally block that prints "Process finished.".
Print the value of fileContent after the try-catch-finally.
๐ก Why This Matters
๐ Real World
In real programs, you often need to handle errors like missing files or network problems. The <code>finally</code> block helps you make sure some code always runs, like closing files or showing messages.
๐ผ Career
Understanding error handling with <code>try-catch-finally</code> is important for writing reliable JavaScript code in jobs like web development, backend programming, and software testing.
Progress0 / 4 steps
1
Create initial variables
Create a variable called fileContent and set it to the string "Hello, world!".
Javascript
Hint
Use let fileContent = "Hello, world!"; to create the variable.
2
Add a file existence flag
Create a variable called fileExists and set it to false.
Javascript
Hint
Use let fileExists = false; to create the flag.
3
Write try-catch-finally blocks
Write a try block that throws an error with the message "File not found" if fileExists is false. Then write a catch block that sets fileContent to "Default content". Finally, write a finally block that prints "Process finished.".
Javascript
Hint
Use try, catch, and finally blocks exactly as described.
4
Print the file content
Write a line to print the value of fileContent after the try-catch-finally block.
Javascript
Hint
Use console.log(fileContent); to print the content.
Practice
(1/5)
1.
What is the main purpose of the finally block in JavaScript?
easy
A. To define variables used in try block
B. To run code that always executes after try and catch blocks
C. To catch errors thrown in the try block
D. To skip the catch block if no error occurs
Solution
Step 1: Understand try-catch-finally structure
The try block runs code that might throw errors, catch handles those errors.
Step 2: Role of finally block
The finally block runs code after try and catch, no matter if an error occurred or not.
Final Answer:
To run code that always executes after try and catch blocks -> Option B
Quick Check:
finally always runs = C [OK]
Hint: finally always runs after try/catch, no matter what [OK]
Common Mistakes:
Thinking finally only runs if there is an error
Confusing catch and finally blocks
Believing finally can catch errors
2.
Which of the following is the correct syntax to add a finally block after a try and catch block?
Hint: catch {} valid in modern JS with optional binding [OK]
Common Mistakes:
Thinking catch always requires parameter
Believing syntax error without catch param
Confusing with pre-ES2019 JS
5.
Consider this code:
let result = '';
function test() {
try {
result += 'Try';
throw new Error('Error!');
} catch (e) {
result += 'Catch';
return result;
} finally {
result += 'Finally';
}
}
test();
console.log(result);
What will be printed to the console?
hard
A. TryCatchFinally
B. TryCatch
C. TryFinally
D. Try
Solution
Step 1: Trace try block execution
result becomes 'Try', then error is thrown.
Step 2: Catch block runs and appends 'Catch', then returns result ('TryCatch')
Before returning, finally runs and appends 'Finally' to result.
Step 3: Check console.log output
console.log prints result after finally appended, so 'TryCatchFinally'.
Final Answer:
TryCatchFinally -> Option A
Quick Check:
finally runs before return completes = B [OK]
Hint: finally runs before return completes, modifies variables [OK]
Common Mistakes:
Assuming return stops finally from running
Expecting console.log to print before finally modifies result
Ignoring that finally runs even after return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.