0
0
Javascriptprogramming~10 mins

Try–catch 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 catch errors using a try-catch block.

Javascript
try {
  console.log('Start');
  [1]
} catch (error) {
  console.log('Error caught');
}
Drag options to blanks, or click blank then click option'
Athrow new Error('Oops!')
Bconsole.log('No error')
Creturn 'Done'
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of throw inside try.
Using break outside loops or switch.
2fill in blank
medium

Complete the code to handle an error and print its message.

Javascript
try {
  JSON.parse('invalid json');
} catch ([1]) {
  console.log([1].message);
}
Drag options to blanks, or click blank then click option'
Ae
Berr
Cexception
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside catch and console.log.
Omitting the catch parameter.
3fill in blank
hard

Fix the error in the try-catch syntax.

Javascript
try {
  let result = 10 / 0;
  console.log(result);
} [1] (e) {
  console.log('Error:', e);
}
Drag options to blanks, or click blank then click option'
Aerror
Bfinally
Ccatch
Dexcept
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'except' which is Python syntax.
Using 'error' which is not a keyword.
4fill in blank
hard

Fill both blanks to rethrow an error after catching it.

Javascript
try {
  throw new Error('Fail');
} [1] (err) {
  console.log('Caught:', err.message);
  [2] err;
}
Drag options to blanks, or click blank then click option'
Acatch
Bthrow
Cfinally
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finally' instead of 'catch'.
Using 'return' instead of 'throw' to rethrow.
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('Invalid JSON:', [3].message);
    return null;
  }
}
Drag options to blanks, or click blank then click option'
Aparse
Berror
Ce
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name instead of parse.
Using different variable names inside catch and console.log.