Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Not using throw causes no error to catch.
Using return inside try without error does not trigger catch.
β Incorrect
The throw statement inside try causes an error that is caught by catch.
2fill in blank
mediumComplete 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different names inside catch and inside the block causes ReferenceError.
Omitting the catch parameter causes syntax error.
β Incorrect
The catch block parameter is commonly named error to access the error object.
3fill in blank
hardFix 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using brackets or braces instead of parentheses causes syntax error.
Omitting parentheses causes syntax error.
β Incorrect
The catch block must have parentheses around the error parameter.
4fill in blank
hardFill 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'
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.
β Incorrect
Use throw to raise the error and access e.message to get the error text.
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('Parsing failed:', [3].message);
return null;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using JSON.stringify instead of parse causes wrong output.
Mismatch between catch parameter and usage causes ReferenceError.
β Incorrect
Use JSON.parse to parse, catch parameter e, and log e.message.