Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of throw inside try.
Using break outside loops or switch.
✗ Incorrect
The throw statement inside try causes an error that is caught by catch.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inside catch and console.log.
Omitting the catch parameter.
✗ Incorrect
The catch block receives the error object, here named 'error', to access its message.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'except' which is Python syntax.
Using 'error' which is not a keyword.
✗ Incorrect
The correct keyword to catch errors is 'catch'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finally' instead of 'catch'.
Using 'return' instead of 'throw' to rethrow.
✗ Incorrect
Use 'catch' to handle the error and 'throw' to rethrow it.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name instead of parse.
Using different variable names inside catch and console.log.
✗ Incorrect
Use JSON.parse to parse JSON, and catch block parameter 'e' to handle errors.