View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  package com.puppycrawl.tools.checkstyle.checks.blocks.emptycatchblock;
20  import java.io.IOException;
21  public class InputEmptyCatchBlockDefault
22  {
23      
24      private void foo() {
25          try {
26              throw new RuntimeException();
27          } catch (Exception expected) {
28              //Expected
29          }
30      }
31      
32      private void foo1() {
33          try {
34              throw new RuntimeException();
35          } catch (Exception e) {}
36          
37      }
38      
39      private void foo2() {
40          try {
41              throw new IOException();
42          } catch (IOException | NullPointerException | ArithmeticException ignore) {
43          }
44      }
45      
46      private void foo3() { // comment
47          try {
48              throw new IOException();
49          } catch (IOException | NullPointerException | ArithmeticException e) { //This is expected
50          }
51      }
52      
53      private void foo4() {
54          try {
55              throw new IOException();
56          } catch (IOException | NullPointerException | ArithmeticException e) { /* This is expected*/
57          }
58      }
59      
60      private void foo5() {
61          try {
62              throw new IOException();
63          } catch (IOException | NullPointerException | ArithmeticException e) { // Some singleline comment
64          }
65      }
66      
67      private void foo6() {
68          try {
69              throw new IOException();
70          } catch (IOException expected) { // This is expected
71              int k = 0;
72          }
73      }
74      
75      public void testTryCatch()
76      {
77          try {
78              int y=0;
79              int u=8;
80              int e=u-y;
81              return; 
82          } 
83          catch (Exception e) {
84              System.identityHashCode(e);
85              return; 
86          }
87          finally
88          {
89              return; 
90          }
91      }
92      
93      public void testTryCatch2()
94      {
95          try {
96          } 
97          catch (Exception e) { //OK
98              //This is expected
99              /* This is expected */
100             /**This is expected */
101         }
102         finally
103         {
104         }
105     }
106     
107     public void testTryCatch3()
108     {
109         try {
110             int y=0;
111             int u=8;
112             int e=u-y;
113         } 
114         catch (IllegalArgumentException e) {
115             System.identityHashCode(e); //some comment
116             return; 
117         }
118         catch (IllegalStateException ex) {
119                 System.identityHashCode(ex);
120                 return; 
121         }
122     }
123     
124     public void testTryCatch4()
125     {
126         int y=0;
127         int u=8;
128         try {
129             int e=u-y;
130         } 
131         catch (IllegalArgumentException e) {
132             System.identityHashCode(e);
133             return; 
134         }
135     }
136     public void setFormats() {
137         try {
138             int k = 4;
139         } catch (Exception e) {
140             Object k = null;
141             if (k != null)
142                 k = "ss";
143             else {
144                 return; 
145             }
146         }
147     }
148     public void setFormats1() {
149         try {
150             int k = 4;
151         } catch (Exception e) {
152             Object k = null;
153             if (k != null) {
154                 k = "ss";
155             } else {
156                 return; 
157             }
158         }
159     }
160     public void setFormats2() {
161         try {
162             int k = 4;
163         } catch (Exception e) {
164             Object k = null;
165             if (k != null) {
166                 k = "ss";
167                 return;
168             } 
169         }
170     }
171     public void setFormats3() {
172         try {
173             int k = 4;
174         } catch (Exception e) {
175             Object k = null;
176             if (k != null) {
177                 k = "ss";
178                 
179             } 
180         }
181     }
182     
183     private void some() {
184         try {
185             throw new IOException();
186         } catch (IOException e) {
187             /* ololo
188              * blalba
189              */
190         }
191     }
192     private void some1() {
193         try {
194             throw new IOException();
195         } catch (IOException e) {
196             /* lalala
197              * This is expected
198              */
199         }
200     }
201     private void some2() {
202         try {
203             throw new IOException();
204         } catch (IOException e) {
205             /*
206              * This is expected
207              * lalala
208              */
209         }
210     }
211     private void some3() {
212         try {
213             throw new IOException();
214         } catch (IOException e) {
215             // some comment
216             //This is expected
217         }
218     }
219     private void some4() {
220         try {
221             throw new IOException();
222         } catch (IOException e) {
223             //This is expected
224             // some comment
225         }
226     }
227     private void some5() {
228         try {
229             throw new IOException();
230         } catch (IOException e) {
231             /* some comment */
232             //This is expected
233         }
234     }
235     
236     private void emptyMultilineComent() {
237         try {
238             throw new IOException();
239         } catch (IOException e) {
240             /*
241 */
242         }
243     }
244 }