0
0
Javascriptprogramming~20 mins

Why error handling is required in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we need error handling in JavaScript?

Which of the following best explains why error handling is important in JavaScript programs?

AIt prevents the program from ever stopping or crashing, no matter what.
BIt makes the program run faster by skipping error checks.
CIt helps the program continue running smoothly even when unexpected problems occur.
DIt automatically fixes all bugs in the code without programmer input.
Attempts:
2 left
πŸ’‘ Hint

Think about what happens if something goes wrong in a program without any way to handle it.

❓ Predict Output
intermediate
2:00remaining
Output with and without error handling

What will be the output of the following JavaScript code?

function divide(a, b) {
  return a / b;
}

console.log(divide(10, 0));
Javascript
function divide(a, b) {
  return a / b;
}

console.log(divide(10, 0));
AError: Division by zero
B0
CNaN
DInfinity
Attempts:
2 left
πŸ’‘ Hint

In JavaScript, dividing a number by zero does not throw an error but returns a special value.

❓ Predict Output
advanced
2:00remaining
What error is thrown without try-catch?

What error will this code produce when run?

const obj = null;
console.log(obj.name);
Javascript
const obj = null;
console.log(obj.name);
ATypeError: Cannot read property 'name' of null
BReferenceError: obj is not defined
CSyntaxError: Unexpected token '.'
Dundefined
Attempts:
2 left
πŸ’‘ Hint

Accessing a property on null causes a specific type of error in JavaScript.

🧠 Conceptual
advanced
2:00remaining
Benefits of using try-catch blocks

Which of the following is NOT a benefit of using try-catch blocks in JavaScript?

AThey automatically fix the errors in the code.
BThey allow the program to handle errors without stopping execution abruptly.
CThey can provide custom error messages to users.
DThey help in debugging by catching and logging errors.
Attempts:
2 left
πŸ’‘ Hint

Think about what try-catch blocks do and what they do not do.

πŸš€ Application
expert
3:00remaining
Predict output with nested try-catch and finally

What will be the output of this JavaScript code?

function test() {
  try {
    console.log('Start');
    throw new Error('Oops');
  } catch (e) {
    console.log('Caught:', e.message);
    return 'From catch';
  } finally {
    console.log('Finally block');
    return 'From finally';
  }
}

console.log(test());
Javascript
function test() {
  try {
    console.log('Start');
    throw new Error('Oops');
  } catch (e) {
    console.log('Caught:', e.message);
    return 'From catch';
  } finally {
    console.log('Finally block');
    return 'From finally';
  }
}

console.log(test());
A
Start
Caught: Oops
Finally block
From catch
B
Start
Caught: Oops
Finally block
From finally
C
Start
Finally block
Caught: Oops
From finally
D
Start
Caught: Oops
From catch
Finally block
Attempts:
2 left
πŸ’‘ Hint

Remember that a return in finally overrides previous returns.