Bird
Raised Fist0
Javascriptprogramming~20 mins

Finally block in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Finally Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of finally block with return
What is the output of this JavaScript code?
function test() {
  try {
    return 'try';
  } finally {
    return 'finally';
  }
}
console.log(test());
Javascript
function test() {
  try {
    return 'try';
  } finally {
    return 'finally';
  }
}
console.log(test());
Atry
Bundefined
Cfinally
DError
Attempts:
2 left
πŸ’‘ Hint
Remember that the finally block runs after try and can override return values.
❓ Predict Output
intermediate
2:00remaining
Finally block execution with thrown error
What will be logged to the console?
try {
  throw new Error('Oops');
} finally {
  console.log('Cleanup');
}
Javascript
try {
  throw new Error('Oops');
} finally {
  console.log('Cleanup');
}
ACleanup\nError: Oops
BNo output
CError: Oops
DCleanup
Attempts:
2 left
πŸ’‘ Hint
The finally block runs even if an error is thrown.
❓ Predict Output
advanced
2:00remaining
Return value with try, catch, and finally
What is the output of this code?
function example() {
  try {
    throw 'error';
  } catch (e) {
    return 'catch';
  } finally {
    return 'finally';
  }
}
console.log(example());
Javascript
function example() {
  try {
    throw 'error';
  } catch (e) {
    return 'catch';
  } finally {
    return 'finally';
  }
}
console.log(example());
Acatch
Bfinally
Cundefined
Derror
Attempts:
2 left
πŸ’‘ Hint
Consider which return statement takes priority.
❓ Predict Output
advanced
2:00remaining
Finally block with thrown error and catch
What will be the output?
try {
  throw 'fail';
} catch (e) {
  console.log('Caught:', e);
  throw 'new fail';
} finally {
  console.log('Finally block');
}
Javascript
try {
  throw 'fail';
} catch (e) {
  console.log('Caught:', e);
  throw 'new fail';
} finally {
  console.log('Finally block');
}
AFinally block
BFinally block\nCaught: fail
CCaught: fail
DCaught: fail\nFinally block
Attempts:
2 left
πŸ’‘ Hint
The finally block runs after catch, even if catch throws again.
🧠 Conceptual
expert
3:00remaining
Effect of finally on thrown error propagation
Consider this code:
function f() {
  try {
    throw 'error';
  } finally {
    return 'finally';
  }
}

try {
  console.log(f());
} catch (e) {
  console.log('Caught:', e);
}

What will be printed to the console?
Afinally
BError: error
Cundefined
DCaught: error
Attempts:
2 left
πŸ’‘ Hint
A return in finally overrides thrown errors.

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

  1. Step 1: Understand try-catch-finally structure

    The try block runs code that might throw errors, catch handles those errors.
  2. Step 2: Role of finally block

    The finally block runs code after try and catch, no matter if an error occurred or not.
  3. Final Answer:

    To run code that always executes after try and catch blocks -> Option B
  4. 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?

try {
  // code
} catch (e) {
  // handle error
} ??? {
  // cleanup code
}
easy
A. cleanup
B. final
C. finally
D. end

Solution

  1. Step 1: Recall JavaScript error handling syntax

    The correct keyword after catch is 'finally' to run code always.
  2. Step 2: Check options for correct keyword

    Only 'finally' is valid JavaScript syntax here.
  3. Final Answer:

    finally -> Option C
  4. Quick Check:

    finally keyword syntax = D [OK]
Hint: finally is the exact keyword after catch for cleanup [OK]
Common Mistakes:
  • Using 'final' or 'end' instead of 'finally'
  • Omitting the finally block keyword
  • Placing finally before catch
3.

What will be the output of this code?

function test() {
  try {
    console.log('Try block');
    return 'Return from try';
  } catch (e) {
    console.log('Catch block');
  } finally {
    console.log('Finally block');
  }
}
console.log(test());
medium
A. Finally block\nReturn from try
B. Try block\nCatch block\nFinally block\nReturn from try
C. Try block\nReturn from try
D. Try block\nFinally block\nReturn from try

Solution

  1. Step 1: Trace the try block execution

    It logs 'Try block' and then hits return 'Return from try'.
  2. Step 2: Understand finally runs after return

    Even after return, finally runs and logs 'Finally block'. Then function returns.
  3. Final Answer:

    Try block\nFinally block\nReturn from try -> Option D
  4. Quick Check:

    finally runs after return = A [OK]
Hint: finally runs even after return in try [OK]
Common Mistakes:
  • Thinking finally does not run after return
  • Expecting catch block to run without error
  • Missing the order of console logs
4.

Find the error in this code snippet:

try {
  throw new Error('Oops');
} catch {
  console.log('Caught error');
} finally {
  console.log('Cleanup');
}
medium
A. No error, code runs fine
B. finally block cannot be used without catch parameter
C. throw statement is incorrect
D. Missing error parameter in catch block

Solution

  1. Step 1: Check catch block syntax

    Optional catch binding (ES2019+) allows catch {} without parameter. Valid in modern JavaScript.
  2. Step 2: Verify execution

    Throws error, catch logs 'Caught error', finally logs 'Cleanup'. No errors.
  3. Final Answer:

    No error, code runs fine -> Option A
  4. Quick Check:

    catch {} valid = D [OK]
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

  1. Step 1: Trace try block execution

    result becomes 'Try', then error is thrown.
  2. Step 2: Catch block runs and appends 'Catch', then returns result ('TryCatch')

    Before returning, finally runs and appends 'Finally' to result.
  3. Step 3: Check console.log output

    console.log prints result after finally appended, so 'TryCatchFinally'.
  4. Final Answer:

    TryCatchFinally -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard