I'm surprised that, if I remove the first assignment, I get a compilation error. The code is the following one:
public void antlr(String fullyQualifiedFilename) {
String path = null;
try {
path = new File(fullyQualifiedFilename).getParent();
if (path != null) {
path = new File(path).getCanonicalPath();
}
} catch (IOException ioe) {
//...
}
if (path != null) {
int f;
}
}
and I want to remove the variable initialisation and keep just the declaration. Indeed:
public void antlr(String fullyQualifiedFilename) {
String path;
try {
path = new File(fullyQualifiedFilename).getParent();
if (path != null) {
path = new File(path).getCanonicalPath();
}
} catch (IOException ioe) {
//...
}
if (path != null) {
int f;
}
}
The error I get is the following:
Example.java:19,9: error: Local variable path is not assigned before used
That make sense expect that the first File
constructor throws only the unchecked exception NullPointerException
and getParent()
does not throw any checked exceptions.
So, the first assignment is always compute expect if an unchecked exception is thrown.
What I mean is that the compiler have all the necessary information to know that the first assignment path = new File(fullyQualifiedFilename).getParent()
is always executed.
This is what I think the Control Flow Graph should be.
Am I wrong ? Am I missing something?