Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to ensure the finally block always runs.
Javascript
try { console.log('Try block'); } [1] { console.log('Catch block'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using catch instead of finally
Forgetting the finally block
Using else which is not valid here
β Incorrect
The finally block runs after try and catch blocks, no matter what. Here, to catch errors, catch is needed before finally.
2fill in blank
mediumComplete the code to log a message in the finally block.
Javascript
try { throw new Error('Oops'); } catch(e) { console.log('Error caught'); } [1] { console.log('Always runs'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using catch again instead of finally
Using else which is invalid
Not including finally block
β Incorrect
The finally block runs after try and catch, even if an error was thrown.
3fill in blank
hardFix the error in the code by completing the block that always runs.
Javascript
try { console.log('Start'); } [1] { console.log('Cleanup'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using catch without error parameter
Using else which is invalid
Omitting finally block
β Incorrect
The finally block runs always, catch needs an error parameter and is for errors only.
4fill in blank
hardFill both blanks to create a try-catch-finally structure.
Javascript
try { console.log('Trying'); } [1] (error) { console.log('Caught error'); } [2] { console.log('Always runs'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Swapping catch and finally
Using else instead of finally
Omitting catch or finally
β Incorrect
catch handles errors, finally runs always after try and catch.
5fill in blank
hardFill all three blanks to handle errors and always run cleanup code.
Javascript
try { [1]('Hello'); } [2] (e) { console.log('Error:', e.message); } [3] { console.log('Done'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using throw inside try without catch
Omitting finally block
Using wrong function instead of console.log
β Incorrect
console.log prints, catch handles errors, finally runs always after try and catch.