Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do try/catch or synchronized in Java require a statement block? [closed]

Tags:

java

jls

Java allows for certain keywords to be followed by a statement or a statement block. For example:

if (true)
    System.out.println("true");

do
    System.out.println("true");
while (true);

compiles as well as

if(true) {
    System.out.println("true");
}

do {
   System.out.println("true");
} while (true);

This is also true for keywords like for, while etc.

However, some keywords don't allow this. synchronized requires a block statement. Same for try ... catch ... finally, which requires at least two block statements following the keywords. For example:

try {
    System.out.println("try");
} finally {
    System.out.println("finally");
}

synchronized(this) {
    System.out.println("synchronized");
}

works, but the following doesn't compile:

try
    System.out.println("try");
finally
    System.out.println("finally");

synchronized (this)
    System.out.println("synchronized");

So why do some keywords in Java require a block statement, while others allow a block statement as well as a single statement? Is this an inconsistency in language design, or is there a certain reason for this?

like image 235
Bob Avatar asked Jun 11 '12 21:06

Bob


People also ask

Why we have to use the synchronized block in Java?

We need to synchronize the shared resources to ensure that at a time only one thread is able to access the shared resource. If an Object is shared by multiple threads then there is need of synchronization in order to avoid the Object's state to be getting corrupted. Synchronization is needed when Object is mutable.

Why We Need a synchronized block to execute wait?

As long as it's there, it sits idle. Note also that wait() forces the thread to release its lock. This means that it must own the lock of an object before calling the wait() method of that (same) object. Hence the thread must be in one of the object's synchronized methods or synchronized block before calling wait().

Why do you need to use synchronized methods or blocks?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

What is the need of declaring synchronized block?

Synchronized block is used to lock an object for any shared resource. Scope of synchronized block is smaller than the method. A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource.


2 Answers

You get a dangling else-like ambiguity if you try to allow leaving out the braces. Whilst this could be solved in a similar fashion to the dangling-else, probably best not to.

Consider

try
try 
fn();
catch (GException exc)
g();
catch (HException exc)
h();
catch (IException exc)
i();

Does that mean

try
    try 
        fn();
    catch (GException exc)
        g();
    catch (HException exc)
        h();
catch (IException exc)
    i();

or

try
    try 
        fn();
    catch (GException exc)
        g();
catch (HException exc)
    h();
catch (IException exc)
    i();

I believe in CLU, catch blocks were around just one statement (may be wrong).

like image 69
Tom Hawtin - tackline Avatar answered Nov 15 '22 14:11

Tom Hawtin - tackline


It's just the design decision of the language, and its compiler mechanics.

I agree with the decision. Not requiring a code block might make the code shorter, but it's a sure fire way to cause confusion and create unforeseen consequences.

like image 44
Jonathan B Avatar answered Nov 15 '22 14:11

Jonathan B