0
0
Javascriptprogramming~10 mins

Catching runtime errors 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 catch errors using try-catch.

Javascript
try {
  console.log('Start');
  [1]
} catch (error) {
  console.log('Error caught');
}
Drag options to blanks, or click blank then click option'
Aconsole.log('No error')
Blet x = 5
Cthrow new Error('Oops!')
Dreturn true
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Not using throw causes no error to catch.
Using return inside try without error does not trigger catch.
2fill in blank
medium

Complete the code to log the error message inside catch.

Javascript
try {
  JSON.parse('invalid json');
} catch ([1]) {
  console.log([1].message);
}
Drag options to blanks, or click blank then click option'
Aerror
Be
Cerr
Dexception
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different names inside catch and inside the block causes ReferenceError.
Omitting the catch parameter causes syntax error.
3fill in blank
hard

Fix the error in the code to properly catch exceptions.

Javascript
try {
  let result = a / 0;
  console.log(result);
} catch [1] {
  console.log('Error:', error.message);
}
Drag options to blanks, or click blank then click option'
A(error)
B[error]
C{error}
Derror
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using brackets or braces instead of parentheses causes syntax error.
Omitting parentheses causes syntax error.
4fill in blank
hard

Fill both blanks to throw and catch a custom error message.

Javascript
try {
  [1] new Error('Custom error');
} catch (e) {
  console.log(e.[2]);
}
Drag options to blanks, or click blank then click option'
Athrow
Bmessage
Cname
Derror
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using error instead of throw does not raise an error.
Accessing e.name instead of e.message shows error type, not message.
5fill in blank
hard

Fill all three blanks to parse JSON safely and handle errors.

Javascript
function safeParse(json) {
  try {
    return JSON.[1](json);
  } catch ([2]) {
    console.log('Parsing failed:', [3].message);
    return null;
  }
}
Drag options to blanks, or click blank then click option'
Aparse
Berror
Ce
Derr
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using JSON.stringify instead of parse causes wrong output.
Mismatch between catch parameter and usage causes ReferenceError.