11package checks ;
22
33import io .vavr .control .Try ;
4+ import java .io .ByteArrayOutputStream ;
45import java .io .IOException ;
6+ import java .io .OutputStream ;
57import java .io .Writer ;
68import java .net .InetAddress ;
79import java .util .function .Consumer ;
@@ -149,6 +151,8 @@ protected void doPut(jakarta.servlet.http.HttpServletRequest request, jakarta.se
149151 }
150152}
151153
154+ // @formatter:on
155+
152156class ShouldNotRaiseIfOuterTryCatchesException extends HttpServlet {
153157 @ Override
154158 protected void doGet (HttpServletRequest request , HttpServletResponse response ) {
@@ -165,3 +169,140 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
165169 }
166170 }
167171}
172+
173+ class ShouldHandleNestedTryCatchConstructs {
174+ static class ShouldDetectUncaughtExceptionInDoublyNestedTryCatch extends HttpServlet {
175+ @ Override
176+ protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws IOException {
177+ try {
178+ try {
179+ throwIOException (); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}}
180+ } catch (ArrayIndexOutOfBoundsException e ) {
181+ }
182+ } catch (IllegalArgumentException e ) {
183+ }
184+ }
185+ }
186+
187+ static class ShouldNotRaiseForCaughtExceptionInDoublyNestedTryCatch extends HttpServlet {
188+ @ Override
189+ protected void doGet (HttpServletRequest request , HttpServletResponse response ) {
190+ try {
191+ try {
192+ throwIOException (); // Compliant
193+ } catch (ArrayIndexOutOfBoundsException e ) {
194+ }
195+ } catch (IOException e ) {
196+ }
197+ }
198+
199+ @ Override
200+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) {
201+ try {
202+ try {
203+ throwIOException (); // Compliant
204+ } catch (IOException e ) {
205+ }
206+ } catch (ArrayIndexOutOfBoundsException e ) {
207+ }
208+ }
209+ }
210+
211+ static class ShouldDetectUncaughtExceptionIfTryCatchIsOnLowerLevel extends HttpServlet {
212+ @ Override
213+ protected void doGet (HttpServletRequest request , HttpServletResponse response ) throws IOException {
214+ try {
215+ throwIOException (); // Compliant
216+ } catch (IOException e ) {
217+ }
218+
219+ throwIOException (); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}}
220+ }
221+
222+ @ Override
223+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws IOException {
224+ throwIOException (); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}}
225+
226+ try {
227+ throwIOException (); // Compliant
228+ } catch (IOException e ) {
229+ }
230+ }
231+
232+ @ Override
233+ protected void doPut (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
234+ try {
235+ throwIOException (); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}}
236+
237+ try {
238+ throwIOException (); // Compliant
239+ } catch (IOException e ) {
240+ }
241+ } catch (ArrayIndexOutOfBoundsException e ) {
242+
243+ }
244+ }
245+ }
246+
247+ static class ShouldDetectForMixedExceptions extends HttpServlet {
248+ @ Override
249+ protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
250+ try {
251+ try {
252+ throwIOException (); // Compliant: Caught by inner try-catch
253+ throwServletException (); // Compliant: Caught by outer try-catch
254+ } catch (IOException e ) {
255+
256+ }
257+
258+ throwIOException (); // Noncompliant {{Handle the following exception that could be thrown by "throwIOException": IOException.}}
259+ } catch (ServletException e ) {
260+
261+ }
262+ }
263+ }
264+
265+ static class ShouldDetectAcrossTryWithResources extends HttpServlet {
266+ @ Override
267+ protected void doPost (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
268+ try {
269+ throwServletException ();
270+ try (OutputStream stream = new ByteArrayOutputStream ()) {
271+ stream .write (42 ); // Noncompliant {{Handle the following exception that could be thrown by "write": IOException.}}
272+ }
273+ } catch (ServletException e ) {
274+
275+ }
276+ }
277+
278+ @ Override
279+ protected void doGet (HttpServletRequest req , HttpServletResponse resp ) {
280+ try {
281+ try (OutputStream stream = new ByteArrayOutputStream ()) {
282+ stream .write (42 ); // Compliant: Caught by outer try-catch
283+ }
284+ } catch (IOException e ) {
285+
286+ }
287+ }
288+
289+ @ Override
290+ protected void doHead (HttpServletRequest req , HttpServletResponse resp ) throws IOException {
291+ try (OutputStream stream = new ByteArrayOutputStream ()) {
292+ try {
293+ stream .write (42 ); // Compliant
294+ } catch (IOException e ) {
295+
296+ }
297+ }
298+ }
299+ }
300+
301+ private static void throwIOException () throws IOException {
302+ throw new IOException ();
303+ }
304+
305+ private static void throwServletException () throws ServletException {
306+ throw new ServletException ();
307+ }
308+ }
0 commit comments