Javascript Developer JSE 40 01 · Free Practice Question Easy
Question 19
Q536 - Error Handling
Analyze the following code:
- try {
- ocnsole.log("start");
- } catch (error) {
- console.log("error");
- }
- console.log("end");
What will happen as a result of its execution?
-
A
The words
"start","error","end"will appearin the console on successive lines.
-
B
In the console, there will appear in successive
lines the words
"error","end" -
C
The following words will appear in the console:
"start","end" -
D
The operation of the program will be interrupted
and the console will display the default message
"Uncaught ReferenceError: ocnsole is not defined"
Reveal correct answer
Correct answer: B
Explanation
Topic: try catch
Try it yourself:
- try {
- ocnsole.log("start");
- } catch (error) {
- console.log("error"); // error
- }
- console.log("end"); // end
- // ocnsole.log("start");
- // Uncaught ReferenceError: ocnsole is not defined
Explanation:
There is a typo in console (it is written ocnsole)
therefore an exception is raised, the catch block is executed
and "error" is printed to the console.
After the try catch statements "end" is printed to the console.
The try catch statements combo handles errors without stopping JavaScript.
https://www.w3schools.com/jsref/jsref_try_catch.asp
(There is a similar question Q636.)
Q536 (Please refer to this number, if you want to write me about this question.)
A. This choice is incorrect because the code will throw a ReferenceError in the try block due to the typo in the variable name. The catch block will handle the error and display "error" followed by "end" in successive lines, but "start" will not be printed to the console.
B. The code will result in an error because there is a typo in the console.log statement (ocnsole instead of console). The try block will throw a ReferenceError, which will be caught by the catch block. Therefore, the console will display "error" followed by "end" in successive lines.
C. This choice is incorrect because the code will not successfully execute the console.log("start") statement due to the typo in the variable name. As a result, "start" will not appear in the console, and only "error" followed by "end" will be displayed.
D. This choice is incorrect because the program will not be interrupted. Instead, the error will be caught by the catch block, preventing the default error message from being displayed. The console will show "error" followed by "end" due to the typo in the code.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
