Reputation: 115
Let's say that I have this block of code inside a method:
...
synchronized (this) {
this.var = value;
}
...
Could there be any scenario that makes that synchronized
block throws an exception (assuming this
, var
, and value
having non null
values)?
Thanks!
Upvotes: 2
Views: 746
Reputation: 13556
According to the Java Language Specification about the synchronized statement :
The executing thread locks the monitor associated with V. Then the Block is executed [...]
And the Java virtual Machine Specification on Synchronization, that explains how the mentioned monitors are to be used, only talks about exceptions being thrown from within the synchronized block. So the synchronized
statement itself isn't permitted to throw an exceptions.
So there can't be any scenario in your case where an exception gets thrown.
Upvotes: 1