More‎ > ‎

Findbugs™ Warnings By Example - Exception Handling & Control Flow

This article describes selected warnings of Findbugs with Java sample code. Here exception handling and control flow related warnings of FindBugs™ are in the center of attention. The following 5 warnings are covered in this tutorial:

- LEST_LOST_EXCEPTION_STACK_TRACE
- RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE
- SF_SWITCH_FALLTHROUGH
- SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH_TO_THROW
- SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH 

Motivation for the 'Findbugs™ Warnings By Example' Series












The FindBugs™ bug descriptions of the online documentation are concise and well written. 

Unfortunately, some parts of the descriptions are not easy to understand (even for experienced Java developers). It can be difficult to understand the exact root cause for a warning and/or to find the correct way of fixing.

To be honest - at least I had problems with some warnings in the last years. Quite often, I found no helping sample code in the web. The main weakness of the bug descriptions is, that it uses seldom sample code to demonstrate the wrong and correct situation. 

Example Code

Findbugs™ (Version 2.0.1-rc2) will show all warnings with the minimum rank 18 (of concern) of the Findbugs™ Eclipse Plugin (Version 2.0.1.20120511). The code has been compiled with JDK 1.6.0_24.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.sw_engineering_candies;

public class FindbugsWarningsByExample3 {

	public static void main(final String[] args) {

		System.out.println("\nFindbugs Sample for LEST_LOST_EXCEPTION_STACK_TRACE ");
		// Confidence: Normal, Rank: Scariest (2)
		// Pattern: LEST_LOST_EXCEPTION_STACK_TRACE
		// Type: LEST, Category: CORRECTNESS (Correctness)
		//
		// WRONG
		try {
			FindbugsWarningsByExample3.lestLostExceptionStackTraceWRONG(null);
		} catch (final IllegalArgumentException e) {
			System.out.println("   - Exception Cause: " + e.getCause());
		}
		// CORRECT
		try {
			FindbugsWarningsByExample3.lestLostExceptionStackTraceCORRECT(null);
		} catch (final IllegalArgumentException e) {
			System.out.println("   - Exception Cause: " + e.getCause());
		}

		System.out.println("\nFindbugs Sample for RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE");
		// Confidence: High, Rank: Scary (9)
		// Pattern: RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE
		// Type: RCN, Category: CORRECTNESS (Correctness)
		//
		// WRONG
		try {
			FindbugsWarningsByExample3.rcnRedundantNullcheckWouldHaveBeenANpeWRONG(null);
		} catch (final NullPointerException e) {
			System.out.println("   - Exception Cause: " + e.getCause());
		}
		// CORRECT
		FindbugsWarningsByExample3.rcnRedundantNullcheckWouldHaveBeenANpeCORRECT(null);

		final int randomInput = 1 + (int) Math.round(Math.random() * 1.0);
		System.out.println("\nFindbugs Sample for SF_SWITCH_FALLTHROUGH");
		// Confidence: Normal, Rank: Of Concern (17)
		// Pattern: SF_SWITCH_FALLTHROUGH
		// Type: SF, Category: STYLE (Dodgy code)
		//
		// WRONG
		FindbugsWarningsByExample3.sfSwitchFallthroughWRONG(randomInput);
		// CORRECT
		FindbugsWarningsByExample3.sfSwitchFallthroughCORRECT(randomInput);

		System.out.println("\nFindbugs Sample for SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH_TO_THROW");
		// Confidence: High, Rank: Scariest (1)
		// Pattern: SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH_TO_THROW
		// Type: SF, Category: CORRECTNESS (Correctness)
		//
		// WRONG
		try {
			FindbugsWarningsByExample3.sfDeadStroreDueToSwitchFallthroughToThrowWRONG(randomInput);
		} catch (final IllegalArgumentException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample3.sfDeadStroreDueToSwitchFallthroughToThrowCORRECT(randomInput);

		System.out.println("\nFindbugs Sample for SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH");
		// Confidence: High, Rank: Scariest (1)
		// Pattern: SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH
		// Type: SF, Category: CORRECTNESS (Correctness)
		//
		// WRONG
		FindbugsWarningsByExample3.sfDeadStroreDueToSwitchFallthroughWRONG(randomInput);
		// CORRECT
		FindbugsWarningsByExample3.sfDeadStroreDueToSwitchFallthroughCORRECT(randomInput);

	}

	private static void lestLostExceptionStackTraceWRONG(final String text) {
		try {
			System.out.println(text.length());
		} catch (final NullPointerException e) {
			// Next line should show LEST_LOST_EXCEPTION_STACK_TRACE
			throw new IllegalArgumentException("Stupid error:" + e);
		}
	}

	private static void lestLostExceptionStackTraceCORRECT(final String text) {
		try {
			System.out.println(text.length());
		} catch (final NullPointerException e) {
			throw new IllegalArgumentException("Stupid error:", e);
		}
	}

	private static void rcnRedundantNullcheckWouldHaveBeenANpeWRONG(final String text) {
		// Next line should show RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE
		System.out.println(text.length());
		if (null == text)
			return;
	}

	private static void rcnRedundantNullcheckWouldHaveBeenANpeCORRECT(final String text) {
		if (null == text)
			return;
		System.out.println(text.length());
	}

	private static void sfSwitchFallthroughWRONG(final int input) {
		switch (input) {
		case 1:
			// Next line should show SF_SWITCH_FALLTHROUGH
			System.out.println("   - enter case 1");
		case 2:
			System.out.println("   - enter case 2");
		}
	}

	private static void sfSwitchFallthroughCORRECT(final int input) {
		switch (input) {
		case 1:
			System.out.println("   - enter case 1");
			break;
		case 2:
			System.out.println("   - enter case 2");
		}
	}

	private static int sfDeadStroreDueToSwitchFallthroughToThrowWRONG(final int input) {

		int result;
		switch (input) {
		case 0:
			System.out.println("   - enter case 0");
			result = 4;
			break;
		case 1:
			System.out.println("   - enter case 1");
			result = 2;
			break;
		case 2:
			System.out.println("   - enter case 2");
			result = 1;
		default:
			// Next line should show
			// SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH_TO_THROW
			throw new IllegalArgumentException();
		}
		return result;
	}

	private static int sfDeadStroreDueToSwitchFallthroughToThrowCORRECT(final int input) {

		int result = 0;
		switch (input) {
		case 1:
			System.out.println("   - enter case 0");
			result = 2;
			break;
		case 2:
			System.out.println("   - enter case 2");
			result = 1;
			break;
		default:
			throw new IllegalArgumentException();
		}
		return result;
	}

	private static void sfDeadStroreDueToSwitchFallthroughWRONG(final int input) {

		int result = 0;
		switch (input) {
		case 1:
			System.out.println("   - enter case 1");
			result = 3;
			break;
		case 2:
			System.out.println("   - enter case 2");
			result = 4;
		default:
			System.out.println("   - enter default");
			// Next line should show SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH
			result = 5;
		}
		System.out.println("   - result=" + result);
	}

	private static void sfDeadStroreDueToSwitchFallthroughCORRECT(final int input) {
		int result = 0;
		switch (input) {
		case 0:
			System.out.println("   - enter case 0");
			result = 3;
			break;
		case 1:
			System.out.println("   - enter case 1");
		default:
			System.out.println("   - enter default");
			result = 5;
			break;
		}
		System.out.println("   - result=" + result);
	}

}

The program should print the following to the standard output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Findbugs Sample for LEST_LOST_EXCEPTION_STACK_TRACE 
   - Exception Cause: null
   - Exception Cause: java.lang.NullPointerException

Findbugs Sample for RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE
   - Exception Cause: null

Findbugs Sample for SF_SWITCH_FALLTHROUGH
   - enter case 2
   - enter case 2

Findbugs Sample for SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH_TO_THROW
   - enter case 2
   - ERROR:null
   - enter case 2

Findbugs Sample for SF_DEAD_STORE_DUE_TO_SWITCH_FALLTHROUGH
   - enter case 2
   - enter default
   - result=5
   - enter default
   - result=5

Sponsored Link