Bug 81545

Summary: "Unreachable code" erroneously reported after if( ... ) { throw ... }
Product: [Eclipse Project] JDT Reporter: Anton Tagunov <atagunov>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: fraenkel
Version: 3.1   
Target Milestone: 3.1 M5   
Hardware: PC   
OS: Windows NT   
Whiteboard:

Description Anton Tagunov CLA 2004-12-17 12:47:57 EST
Eclipse Platform
Version: 3.1.0
Build id: 200411050810

How to reproduce: compile this class

public class A
{
	void foo( int i ) throws Exception
	{
		if( i != 1 );
		{
			throw new Exception();
		}
	
		System.out.println( "bar" );
	}
}

Observed behavior: 'System.out.println( "bar" );' causes "Unreachable code" error.

Expected behavior: the code should compile w/o any errors.
Comment 1 Michael Fraenkel CLA 2004-12-21 22:46:57 EST
The error is correct.
You have a semicolon as the then clause for the if hence the throws clause
always occurs.  This makes any statements following unreachable.

If you try your code on java compiler it too should and does report the same error.
Comment 2 Frederic Fusier CLA 2004-12-22 04:43:28 EST
Thanks Michael to have pointed this :-)
Comment 3 Philipe Mulet CLA 2004-12-22 04:47:25 EST
Thanks Michael, the compiler is indeed right.
Also note that one can enable an optional compiler warning to detect empty
control flow statements like this one (which are usually just mistakes):
Preferences>Java>Compiler>Potential programming problems>Empty statement