A source file consists of,in order:

  1. License or copyright information, if present
  2. Package statement
  3. Import statements
  4. Exactly one top-level class

Exactly one blank lineseparates each section that is present.

The package statement isnot line-wrapped. The column limit (Section 4.4,Column limit: 100) does not apply to package statements.

 // This is not acceptable: No concise empty blocks in a multi-block statement
  try {
    doSomething();
  } catch (Exception e) {}

Every variable declaration (field or local) declares only one variable: declarations such asint a, b;are not used.

Exception:Multiple variable declarations are acceptable in the header of aforloop.

Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example,

com.example.deepspace

, not

com.example.deepSpace

or

com.example.deep_space

.

Caught exceptions: not ignored

Except as noted below, it is very rarely correct to do nothing in response to a caught exception. (Typical responses are to log it, or if it is considered "impossible", rethrow it as anAssertionError.)

When it truly is appropriate to take no action whatsoever in a catch block, the reason this is justified is explained in a comment.

try {
  int i = Integer.parseInt(response);
  return handleNumericResponse(i);
} catch (NumberFormatException ok) {
  // it's not numeric; that's fine, just continue
}
return handleTextResponse(response);

Exception:

In tests, a caught exception may be ignored without comment

if

its name is or begins with

expected

. The following is a very common idiom for ensuring that the code under test

does

throw an exception of the expected type, so a comment is unnecessary here.

try {
  emptyStack.pop();
  fail();
} catch (NoSuchElementException expected) {
}

results matching ""

    No results matching ""