views:

62

answers:

2

What's 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?

+3  A: 

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

fastcodejava
+5  A: 

The old java code conventions would argue against the first one, as it extends past 80 characters.

Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

The Java code convention does not distinguish between long throws and other reasons for long method signatures. I would suggest that the second and third are equivalent because they both:

  • Break after a comma.
  • Align the new line with the beginning of the expression at the same level on the previous line.

I would vote for the former, as it is easier to read. To be honest, I'd hate to come across that method.

The real rule of thumb, of course, is to follow the code convention of the team you are working in. If they all use editors that can support 81+ character lines and prefer non-wrapped lines to save vertical real estate, that should be formatting style you use.

akf
+1 for citing coding convention and not just personal opinion.
polygenelubricants
+1 Kind of answer I was looking for. I'll skim through the document to see if there's something specific to the throws.
OscarRyz