Howdevelopersuseexceptionhandlingin Java
Howdevelopersuseexceptionhandlingin Java
net/publication/327248765
CITATIONS READS
33 1,698
4 authors, including:
Chanchal K. Roy
University of Saskatchewan
257 PUBLICATIONS 8,693 CITATIONS
SEE PROFILE
All content following this page was uploaded by Muhammad Asaduzzaman on 18 May 2020.
516
4×106
Throwable
Number of examples
3
Exception Error
(Checked) Unchecked 2
517
Table 1: Patterns of expressions used for exception chaining
Expression Type Percent Example
Cast 1.0215 catch(CustomException ex) { throw (UnsupportredEncodingException) ex; }
catch(Exception ex) { error code==0 ?
Conditional 0.0535 throw new RuntimeException(“Error message”,ex):
throw new Exception(“Another message”,ex); }
Method Call 5.8479 catch(Expression ex) { throw InvocationTargetException.getCause(); }
New 76.1696 catch(Expression ex) { throw new Exception(“Additional error message”,ex); }
Variable Access 16.8920 catch(Expression ex) { throw ex; }
Assignment 0.0147 catch(Expression ex) { throw lastException = new KeyStrokeException(ex); }
Null 0.0008 catch(Expression ex) { return null; }
allows applications to propagate exceptions up the call stack interface (classes that want to take advantage of try-with-
and at the same time preserve important error information. resources need to implement the close method of the Auto-
To use exception chaining an application uses the throw Closeable interface). We are interested in finding whether
statement to throw an exception in response to another ex- developers use these new features in their code. Fig 3 shows
ception. We investigated the patterns developers used to how many files incorporated a new feature each month. From
throw exceptions. This data can help other developers learn the figure we can see that not only do developers use the new
the various ways throw statements are constructed. Ta- features, but also developers start using these features long
ble 1 shows the different expression types developers used before their release (these features were officially released in
to throw exceptions in response to another exception. We July 2011).
see that the largest number of throw statements in excep-
tion chaining throws an exception with a new exception ob- 4.5 Does developer experience affect exception
ject that incorporates an additional error message and may handling coding practice?
also contain a reference to the lower level exception. Condi- We are interested in finding out how developer experience
tional expressions can be used to throw different expression affects bad exception handling coding practices. To calcu-
objects with different error message information depending late developer experience, we use the total number of revi-
on some conditions. Assignment and null expressions are sions committed to the source code by a developer up to a
very infrequent. Variable access is the second most com- particular point in time. In our analysis, we use September
mon expression type used and method call is the third most 2015 as the particular point in time. We calculate the expe-
common expression type used in exception chaining. rience using the weighted mean of the number of committed
revisions of all contributing developers as per Mockus and
4.3 How do developers define their own ex- Weiss [7]. The weight is the proportion of the commit of a
ceptions? particular project and experience is their general experience
Developers can define their own exceptions by extending at that particular time. Here, we categorize developers in
any subclasses of Throwable. Since unchecked exceptions five categories using their experience calculated by the above
(RuntimeException, Error and their subclasses) do not process and they are: 1) Novice; 2) Beginner; 3) Competent;
require to fulfill catch or specify requirements it may be the 4) Proficient; and, 5) Expert. Then, for each bad exception
case that developers are tempted to create all exceptions handling coding practice we determine the ratio of bad code
by extending RuntimeException. The official documenta- examples to total examples for all five developer experience
tion of Java suggests that if a client can expect to recover groups. Results from our study (see Fig. 4) shows that re-
from an exception, make it a checked exception; otherwise gardless of the experience all developers exhibit improper
make it an unchecked exception. We are interested to find exception handling coding practices. However, for IE, CUE
out how developers create their own exceptions. The data and UGEH improper exception handling categories, novice
can show us which options developers prefer to use in prac- developers contribute the most.
tice. Interestingly when we investigate this in the GitHub
data set we found that in majority of the cases developers 5. RELATED WORK
define their own exception classes by extending Exception. A number of studies have been conducted on exception
This indicates that when developers create their own ex- handling. Weimer and Necula [6] used data flow analysis
ceptions, they are concerned about error recovery. We also to find and characterize exception handling mistakes in re-
observe evidence where developers create exception classes source management. Cabral and Marques [8] analyzed 32
by extending RuntimeExcepion, Throwable or Error, Java and .NET applications and found that exception han-
but those cases are very few in number. dling is not used as an error handling tool; when exceptions
occur applications take different recovery actions. Thum-
4.4 When do developers use new exception han- malapenta and Xie [3] developed a technique that mines
dling features? exception handling rules as a sequence of association rules.
Java SE 7 introduced a number of changes to the excep- In another study [4] they reported that exception handling
tion handling mechanism. These included catching multiple actions can be conditional and may need to accommodate
exceptions by using a single catch statement (also knows as exceptional cases. However, none of these studies explore
multi-catch), a try-with-resources declaration that frees de- improper exception handling coding practices or when new
velopers from explicitly closing resources, an AutoCloseable exception handling features are introduced in the code. The
518
Ratio of improper exception handling coding examples
0.4
Novice
Beginner
Competent
0.2
50
0.1
0
2009 2010 2011 2012 2013 2014 2015
First Uses, by File
3 0
250×10 IE CUE NPOE UGEH CE
Improper Exception Handling Categories
200
Multi-Catch Figure 4: The ratio of different improper exception
150 handling coding examples to total examples for dif-
ferent developer experience groups
Files
100
7. CONCLUSION
50 In this paper we investigated exception handling in Java
using the ultra large scale data set (274k Java projects) pro-
0
2004 2006 2008 2010 2012 2014 2016
vided by the Boa infrastructure. We use the Boa language
First Uses, by File and infrastructure for both creating queries and collecting
3
250×10 answers to our research questions. Our study reveals that
improper exception handling coding practice is not uncom-
200 Try-With-Resources mon in Java applications and regardless of experience all
developers use them. Through our investigation on how de-
150
velopers use exception chaining and how they define their
Files
8. REFERENCES
Figure 3: How many files incorporated a new excep- [1] R. Dyer, H. A. Nguyen, H. Rajan, T. N. Nguyen, “Boa:
tion handling feature each month a language and infrastructure for analyzing
ultra-large-scale software repositories”, in Proc. of
most relevant study to ours is the work of Dyer et al. [2] ICSE, 2013, pp. 422-431.
that used the Boa infrastructure to analyze the use of Java [2] R. Dyer, H. Rajan, H. A. Nguyen, T. N. Nguyen,
language features over time. While their work was to deter- “Mining billions of AST nodes to study actual and
mine how new features are adopted once released in Java, potential usage of Java language features”, in Proc. of
we focused our attention on how developers use exception ICSE, 2013, pp. 779-790.
handling in Java. [3] S. Thummalapenta and T. Xie, “Mining exception
handling rules as sequence association rules”, in Proc.
of ICSE, 2013, pp. 496-506.
6. THREATS TO VALIDITY [4] T. Xie and S. Thummalapenta, “Making exceptions on
There are a number of threats to this study. First, we exception handling”, in Proc. of WEH, 2013, pp. 1-3.
created programs in the Boa language to collect answers to [5] J. Bloch, “Effective Java”, 2nd Edition (The Java
our research questions. Although we cannot guarantee that Series), Addison-Wesley, 2008.
our implementation does not contain errors, we spent a con- [6] W. Weimer and G. C. Necula, “Finding and preventing
siderable amount of time testing to minimize the possibility run-time error handling mistakes”, in Proc. of
of introducing errors. Second, in this study we investigated OOPSLA, 2004, pp. 419-431, 2004.
open source Java projects on GitHub to determine whether [7] A. Mockus and D. M. Weiss, “Predicting risk of
developers exhibit improper exception handling coding prac- software changes”, Bell Labs Technical Journal, 5(2),
tices or not. This could be due to the limitation of the pro- 2000, pp. 169-180.
gramming language or application requirements. However, [8] B. Cabral and P. Marques, “Exception handling: a field
we did not investigate the reasons behind improper use of ex- study in Java and .NET”, in Proc. of ECOOP, 2007,
ception handling in this study. Third, we only studied open pp. 151-175.
source Java projects in this study. Therefore, we are unable
2
to generalize our result to non-open source Java projects. https://asaduzzamanparvez.wordpress.com/researchall/
519