View Javadoc
1   package com.github.sevntu.checkstyle.checks.coding;
2   
3   public class InputAvoidHidingCauseExceptionCheck
4   {
5       public Exception fakeException = new Exception();
6       
7       public void Simple()
8       {
9           RuntimeException r = new RuntimeException();
10          try {
11          }
12          catch (ClassCastException  e) {
13              //your code
14          }
15  
16          catch (IndexOutOfBoundsException e) {
17              //your code
18              throw new RuntimeException();  // !
19          }
20  
21          catch (IllegalStateException e) {
22              //your code
23              throw new RuntimeException("Runtime Ecxeption!"); // !
24          }
25  
26          catch (java.lang.ArithmeticException e) {
27              //your code
28              throw new RuntimeException("Runtime Ecxeption!", e);
29          }
30  
31          catch (RuntimeException e) {
32              //your code
33              throw e;
34          }
35  
36          catch (java.lang.Exception e) {
37              //your code
38              throw r;  // !
39          }
40  
41      }
42      
43      public void Stronger()
44      { 
45          boolean x = false;
46          RuntimeException r = new RuntimeException();
47          try {       
48  
49          }
50          catch (IndexOutOfBoundsException e) { 
51              //your code
52              if (x&x | !x) {
53                  while (!!!!!!!!x) {
54                      for (int ee = 0 ; ee < 10; ee++)
55                      throw new RuntimeException(); // !
56                  }
57              }
58          }
59          
60          catch (IllegalStateException e) {
61              while (!!!!!!!!x) {
62                  x = !!!!!!!!false & !!!!!!!!true;
63                  double kkk = Math.pow(5, 25555555);
64                  int ee = (int) kkk;
65                  throw new RuntimeException("Runtime Ecxeption!"); // !            
66              }
67  
68          }
69          catch (java.lang.ArithmeticException e) {
70              int []err = new int [50];
71              if (err[51]==0) { err[999]++; }
72              throw new RuntimeException("Runtime Ecxeption!", e);
73          }
74  
75          catch (RuntimeException e) {
76              for(int a = 0, b = 3; a < 6*a+b; a+= a-2) {
77                  throw e;
78              }            
79          }
80          
81          catch (java.lang.Exception e) { 
82              int []err = new int [50];
83              int []err2 = new int [50];
84              for (int m : err2) {
85                  throw r; // !
86              }
87          }
88         
89      }
90      
91  
92      public void TestNestedANDNotSimple()
93      {
94          RuntimeException myOwnException = new RuntimeException();
95          try {
96          } 
97          catch (ClassCastException e) { // nested: good --> bad
98              //your code
99              try {
100             } catch (RuntimeException n) {
101                 throw new RuntimeException(n); //
102             }
103             throw new RuntimeException(); // !!!!!
104         }
105 
106         catch (IndexOutOfBoundsException e) { // nested: bad --> good            
107             //your code
108             try {
109             } catch (RuntimeException n) {
110                 throw new RuntimeException(myOwnException); // !!!!
111             }            
112             throw new RuntimeException(e); //
113         }
114 
115         catch (NullPointerException e) { // nested: IDENT.getMessage() situation 
116             // with good and bad reaction and DOT situation
117 
118             // your code
119             try {
120                 try {
121                 } catch (RuntimeException x) {
122                     //your code
123                     throw new RuntimeException(x.getMessage(), e); // !!!!
124                 }
125             } catch (java.lang.ArithmeticException x) {
126                 //your code
127                 throw new RuntimeException(e.getMessage(), x); //
128             }
129             throw new RuntimeException(e.getMessage()); // !!!!!
130 
131         }
132         catch (java.lang.ArithmeticException e) { // tests situation #5
133             ArithmeticException ex = null;
134             ArithmeticException ex1 = null;
135             if (e instanceof ArithmeticException) {
136                 ex = (ArithmeticException) e; // bad
137              // ex1 = (ArithmeticException) e; // good
138                 throw ex; //
139             } else {
140                 ex = new ArithmeticException(e.getMessage());
141             }
142             throw ex1; // !!!!!
143 
144         }
145         catch (IllegalArgumentException e) {
146             // your code
147             RuntimeException modelEx = new RuntimeException(e);
148             RuntimeException modelEx2 = null;// = new RuntimeException(e);
149             if (modelEx != null) {
150                 throw modelEx; //
151             }
152             throw new RuntimeException("Exception on set property to value! " +modelEx2.getMessage(), modelEx2); // !!!!!
153         }
154         catch (RuntimeException e) {
155             RuntimeException sqlEx = new RuntimeException("failed to open DB connection to: " + e);
156             try {
157                 sqlEx.initCause(e);
158             } catch (Exception e2) {
159                 // ignore
160             }
161             throw sqlEx; //
162         }
163         catch (Exception e) {
164             RuntimeException ex = null;
165             RuntimeException ex4 = null;
166             if (e instanceof RuntimeException) {
167                 ex = (RuntimeException) e; // null;
168             } else {
169                 ex4 = new RuntimeException(e);
170                 ex4 = null; // No warning. You can change this if you really need.
171             }
172             throw ex4;
173         }
174         catch (Throwable e) { // nested try/catch + 
175             RuntimeException ex = null;
176             try {
177             } catch (RuntimeException e2) {
178                 RuntimeException ex4 = null;
179                 if (ex4 instanceof RuntimeException) {
180                     ex4 = (RuntimeException) e2;
181                     ex = (RuntimeException) e;
182                 }
183                 throw ex4; //
184             }
185 
186             if (e instanceof RuntimeException) {
187                 
188             }
189             throw ex; //
190         }
191     }
192 
193     public void trickyExamples() throws Exception
194     {
195         try {
196         } catch (IndexOutOfBoundsException fakeException) {
197             throw new RuntimeException(this.fakeException);
198         }
199 
200         try {
201         } catch (IndexOutOfBoundsException ee) {
202             throw new RuntimeException(exceptionWrapper(ee));
203         }
204 
205         try {
206         } catch (ClassCastException e) {
207             throw exceptionWrapper(e);
208         }
209 
210         try {
211         } catch (ClassCastException e) {
212             throw this.exceptionWrapper(e);
213         }
214         try {
215         } catch (ClassCastException e) {
216             throw InputAvoidHidingCauseExceptionCheck.this.exceptionWrapper(null);
217         }
218 
219         try {
220         } catch (ClassCastException e) {
221             throw (ClassCastException) e.getCause();
222         }
223 
224         try {
225         } catch (ClassCastException e) {
226             throw new java.util.ArrayList<java.lang.Exception>().get(0);
227         }
228 
229         try {
230         } catch (final ClassCastException e) {
231             // e is not populated in throw block, should be reported an problem 
232             // but it is probably to complicated to be detected by CheckStyle
233             throw new Exception() {
234                 public boolean equals(Object o) {
235                     try {
236                     } catch (Exception e2) {
237                         return true;
238                     }
239                     Exception myException = e;
240                     return false;
241                 }
242             };
243         }
244     }
245 
246     private static Exception exceptionWrapper(Throwable e)
247     {
248         return new IllegalArgumentException(e);
249     }
250 }