0
0
Javascriptprogramming~10 mins

Finally block in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acatch
Bfinally
Celse
Dthrow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using catch instead of finally
Forgetting the finally block
Using else which is not valid here
2fill in blank
medium

Complete 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'
Acatch
Bfinally
Celse
Dthrow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using catch again instead of finally
Using else which is invalid
Not including finally block
3fill in blank
hard

Fix 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'
Afinally
Bcatch
Celse
Dcatch(e)
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using catch without error parameter
Using else which is invalid
Omitting finally block
4fill in blank
hard

Fill 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'
Acatch
Bfinally
Cthrow
Delse
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Swapping catch and finally
Using else instead of finally
Omitting catch or finally
5fill in blank
hard

Fill 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'
Aconsole.log
Bcatch
Cfinally
Dthrow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using throw inside try without catch
Omitting finally block
Using wrong function instead of console.log