Java style for long throw exception list

What is the Java style for formatting a long throws list?

Let's say I have this:

  public void some() throws IOException, ClassNotFoundException, NoSuchMethodException,InvocationTargetException, IllegalAccessException { } 

Should it be:

  public void some() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { } 

  public void some() throws IOException,ClassNotFoundException, NoSuchMethodException,InvocationTargetException, IllegalAccessException { } 

Or something else?

+6
java coding-style sun-coding-conventions
source share
2 answers

The old java code conventions will contradict the first as it contains more than 80 characters.

Avoid strings longer than 80 characters, as many terminals and tools are poorly handled.

In Java code, there is no difference between long throws and other reasons for long method signatures. I would suggest that the second and third are equivalent, because they are both:

  • Break after the decimal point.
  • Align the new line with the beginning of the expression at the same level in the previous line.

I would vote for the first, as it is easier to read. Honestly, I would not want to come across this method.

Of course, the real rule of thumb is to follow the code agreement of the team in which you work. If they all use editors that can support 81+ character strings and prefer not wrapped strings to preserve vertical real estate, this should be the formatting style you use.

+7
source share

I would prefer the second version, it is clear and understandable. One item per line.

+3
source share

All Articles