View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="JavadocMethod">
5         <property name="validateThrows" value="true"/>
6       </module>
7     </module>
8   </module>
9   */
10  package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocmethod;
11  
12  import java.io.File;
13  import java.io.FileNotFoundException;
14  import java.io.IOException;
15  import java.util.Scanner;
16  
17  // xdoc section -- start
18  public class Example7 {
19  
20    /**
21     * Actual exception thrown is child class of class that is declared in throws.
22     * It is limitation of checkstyle (as checkstyle does not know type hierarchy).
23     * Javadoc is valid not declaring FileNotFoundException
24     * BUT checkstyle can not distinguish relationship between exceptions.
25     * @param file some file
26     * @throws IOException if some problem
27     */
28    public void doSomething8(File file) throws IOException {
29      if (file == null) {
30        // violation below, 'Expected @throws tag for 'FileNotFoundException''
31        throw new FileNotFoundException();
32      }
33    }
34  
35    /**
36     * Exact throw type referencing in javadoc even first is parent of second type.
37     * It is a limitation of checkstyle (as checkstyle does not know type hierarchy).
38     * This javadoc is valid for checkstyle and for javadoc tool.
39     * @param file some file
40     * @throws IOException if some problem
41     * @throws FileNotFoundException if file is not found
42     */
43    public void doSomething9(File file) throws IOException {
44      if (file == null) {
45        throw new FileNotFoundException();
46      }
47    }
48  
49    /**
50     * Ignore try block, but keep catch and finally blocks.
51     *
52     * @param s String to parse
53     * @return A positive integer
54     */
55    public int parsePositiveInt(String s) {
56      try {
57        int value = Integer.parseInt(s);
58        if (value <= 0) {
59          throw new NumberFormatException(value + " is negative/zero"); // ok, try
60        }
61        return value;
62      } catch (NumberFormatException ex) {
63        // violation below, 'Expected @throws tag for 'IllegalArgumentException''
64        throw new IllegalArgumentException("Invalid number", ex);
65      } finally {
66        // violation below, 'Expected @throws tag for 'IllegalStateException''
67        throw new IllegalStateException("Should never reach here");
68      }
69    }
70  
71    /**
72     * Try block without catch is not ignored.
73     *
74     * @return a String from standard input, if there is one
75     */
76    public String readLine() {
77      try (Scanner sc = new Scanner(System.in)) {
78        if (!sc.hasNext()) {
79          // violation below, 'Expected @throws tag for 'IllegalStateException''
80          throw new IllegalStateException("Empty input");
81        }
82        return sc.next();
83      }
84    }
85  
86    /**
87     * Lambda expressions are ignored.
88     *
89     * @param s a String to be printed at some point in the future
90     * @return a Runnable to be executed when the string is to be printed
91     */
92    public Runnable printLater(String s) {
93      return () -> {
94        if (s == null) {
95          throw new NullPointerException();
96        }
97        System.out.println(s);
98      };
99    }
100 }
101 // xdoc section -- end