Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java style for long throws exception list

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?

like image 803
OscarRyz Avatar asked Jun 05 '10 02:06

OscarRyz


2 Answers

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.

like image 53
akf Avatar answered Oct 22 '22 02:10

akf


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

like image 21
fastcodejava Avatar answered Oct 22 '22 02:10

fastcodejava