Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What happens to post-increment when an exception is thrown?

exception happens thrown
0
Posted

What happens to post-increment when an exception is thrown?

0

array[i++] = foo(); and foo() throws an exception, i will be incremented anyway. This can cause problems if sometimes foo() throws an exception and you don’t want i incremented in cases when it does. This is a consequence of JLS 15.25.1 and 15.6.1 “the left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.” (assignment is taken as a binary operator). Note that this is not how C++ behaves.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123