Javascript Developer JSE 40 01 · Free Practice Question Medium

Question 20

Q237 - Error Handling


Analyze the following code:



What will happen as a result of its execution?


  • A

    The following words will appear in the console: "start", "end"

  • B

    The words "start", "error", "end"

    will appear in the console on the following lines.

  • C

    The word "error" will appear in the console.

  • D

    The following words will appear in the console: "error", "end"

Reveal correct answer

Correct answer: D

Explanation

Topics: try catch finally postfix increment operator


Try it yourself:


Explanation:

To try to increment a constant will fail and raise a TypeError

Therefore the catch block would be executed and "error" would be displayed.

The finally block always gets executed and will display "end"


The try catch finally statements combo handles errors

without stopping JavaScript.

https://www.w3schools.com/jsref/jsref_try_catch.asp


The increment operator increments its operand and returns a value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment


(There is a similar question Q437.)


Q237 (Please refer to this number, if you want to write me about this question.)

A. The code inside the try block runs successfully, incrementing the value of 'a' and printing "start" to the console. The finally block always executes, printing "end" to the console. The catch block is not executed since there is no error thrown. Therefore, the console will display "start", "end".

B. The code snippet does not throw an error, so the catch block is not executed. The code inside the try block runs successfully, incrementing the value of 'a' and printing "start" to the console. The finally block always executes, printing "end" to the console. Therefore, the console will display "start", "end".

C. Since there is no error thrown in the try block, the catch block is not executed. The code inside the try block runs successfully, incrementing the value of 'a' and printing "start" to the console. The finally block always executes, printing "end" to the console. Therefore, the console will display "start", "end".

D. The code snippet does not throw an error, so the catch block is not executed. The code inside the try block runs successfully, incrementing the value of 'a' and printing "start" to the console. The finally block always executes, printing "end" to the console. Therefore, the console will display "start", "end".

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need