0
0
Node.jsframework~10 mins

Try-catch for synchronous errors in Node.js - Interactive Code Practice

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

Complete the code to catch errors thrown inside the try block.

Node.js
try {
  JSON.parse('invalid json');
} catch ([1]) {
  console.log('Error caught');
}
Drag options to blanks, or click blank then click option'
Ae
Berr
Cerror
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving the catch parentheses empty
Using a reserved word as the error variable
2fill in blank
medium

Complete the code to throw a custom error inside the try block.

Node.js
try {
  throw new [1]('Something went wrong');
} catch (e) {
  console.log(e.message);
}
Drag options to blanks, or click blank then click option'
AError
BException
CSyntaxError
DTypeError
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception which is not a built-in JavaScript error class
Using specific error types when a general error is intended
3fill in blank
hard

Fix the error in the catch block parameter to properly catch the error.

Node.js
try {
  JSON.parse('bad');
} catch [1] {
  console.log('Caught an error');
}
Drag options to blanks, or click blank then click option'
Aerror
B[error]
C(error)
D{error}
Attempts:
3 left
💡 Hint
Common Mistakes
Using square or curly brackets instead of parentheses
Omitting parentheses entirely
4fill in blank
hard

Fill both blanks to catch a TypeError and log its message.

Node.js
try {
  null.[1]();
} catch (e) {
  if (e instanceof [2]) {
    console.log(e.message);
  }
}
Drag options to blanks, or click blank then click option'
AtoString
BTypeError
CReferenceError
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that exists on null
Checking for the wrong error type
5fill in blank
hard

Fill all three blanks to create a try-catch that throws and catches a SyntaxError.

Node.js
try {
  throw new [1]('Invalid syntax');
} catch ([2]) {
  if ([2] instanceof [3]) {
    console.log([2].message);
  }
}
Drag options to blanks, or click blank then click option'
ASyntaxError
Berr
CError
De
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic Error instead of SyntaxError
Using inconsistent variable names in catch and inside the block