0% found this document useful (0 votes)
21 views5 pages

Howdevelopersuseexceptionhandlingin Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Howdevelopersuseexceptionhandlingin Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/327248765

How developers use exception handling in Java?

Conference Paper · March 2017


DOI: 10.1145/2901739.2903500

CITATIONS READS
33 1,698

4 authors, including:

Muhammad Asaduzzaman Md. Ahasanuzzaman


Queen's University Queen's University
25 PUBLICATIONS 887 CITATIONS 13 PUBLICATIONS 176 CITATIONS

SEE PROFILE SEE PROFILE

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.

The user has requested enhancement of the downloaded file.


2016 IEEE/ACM 13th Working Conference on Mining Software Repositories

How Developers Use Exception Handling in Java?

Muhammad Muhammad Chanchal K. Roy


Asaduzzaman Ahasanuzzaman University of Saskatchewan
University of Saskatchewan University of Dhaka [email protected]
[email protected] [email protected]
Kevin A. Schneider
University of Saskatchewan
[email protected]

ABSTRACT to be enclosed in a try statement that catches those ex-


Exception handling is a technique that addresses exceptional ceptions. The try statement is supported by catch and
conditions in applications, allowing the normal flow of ex- finally clauses that contain instructions specifying the ac-
ecution to continue in the event of an exception and/or to tions needing to be taken when the exception occurs. Excep-
report on such events. Although exception handling tech- tion handling offers a number of advantages. This includes
niques, features and bad coding practices have been dis- separating error handling code from the main logic, differ-
cussed both in developer communities and in the literature, entiating and grouping different exceptional situations and
there is a marked lack of empirical evidence on how devel- enabling programs to deal with errors.
opers use exception handling in practice. In this paper we It is required that developers follow the suggested guide-
use the Boa language and infrastructure to analyze 274k line in the Java Language specifications 1 to enjoy the ben-
open source Java projects in GitHub to discover how devel- efits of exception handling. While a number of techniques
opers use exception handling. We not only consider vari- have been developed to identify causes of exceptions, sug-
ous exception handling features but also explore bad coding gesting exception handling code or to identify web discus-
practices and their relation to the experience of developers. sions pertaining to exceptions, there is a marked lack of em-
Our results provide some interesting insights. For example, pirical evidence of how developers use exception handling
we found that bad exception handling coding practices are in practice. In this paper we utilize the Boa language and
common in open source Java projects and regardless of ex- infrastructure [1] to answer questions regarding how devel-
perience all developers use bad exception handling coding opers use exception handling in Java. These questions are
practices. selected to explore bad exception handling coding practices,
their relationship to the experience of developers, using ex-
ception chaining, defining custom exception classes and us-
CCS Concepts ing new exception handling features.
•Software and its engineering → Error handling and The remainder of the paper is organized as follows. Sec-
recovery; tion 2 describes the data set used in our study. Section 3
briefly explains the exception handling technique in Java.
Keywords Section 4 presents our research questions including the re-
sults of our empirical study. Section 5 summarizes the re-
Java; exception; language feature; source code mining lated work and Section 6 discusses threats to this study.
Finally, Section 7 concludes the paper.
1. INTRODUCTION
An exception is an exceptional event that occurs dur- 2. DATA SET
ing the execution of a program and can disrupt the nor- The data set used in this study is the 2015 GitHub data
mal flow of execution. To enable programmers to deal with set from Boa. This includes all Java projects on GitHub with
such exceptional situations, modern programming languages at least one or more Git repositories. The GitHub data set
have built-in support for exception handling. For example, represents 274k projects with 22 million revisions by 320k
the Java programming language uses several language con- developers. It consists of more than 120 million files and over
structs (such as try, catch, finally and throw) to support ex- 20 million of them are unique Java source files. Since we are
ception handling. Code that might throw exceptions needs interested in finding how developers are handling exceptions
in Java without any constraints, we include small projects
Permission to make digital or hard copies of all or part of this work for personal or as well as large ones.
classroom use is granted without fee provided that copies are not made or distributed
for profit or commercial advantage and that copies bear this notice and the full cita-
tion on the first page. Copyrights for components of this work owned by others than
ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or re-
3. EXCEPTION HANDLING IN JAVA
publish, to post on servers or to redistribute to lists, requires prior specific permission This section briefly describes exception handling in Java.
and/or a fee. Request permissions from [email protected]. The language supports three different kinds of exceptions.
MSR’16, May 14-15, 2016, Austin, TX, USA These include checked exception, unchecked exception, and
© 2016 ACM. ISBN 978-1-4503-4186-8/16/05. . . $15.00
1
DOI: http://dx.doi.org/10.1145/2901739.2903500 https://docs.oracle.com/javase/specs/

516
4×106
Throwable

Number of examples
3
Exception Error
(Checked) Unchecked 2

RuntimeException Other Checked Other Unchecked


(Unchecked) Exceptions Exceptions 0
IE CUE NPOE UGEH CE
Improper exception handling practices
Figure 1: Exception Hierarchy in Java
error (see Fig. 1). Checked exceptions are those exceptions Figure 2: The frequency of different improper ex-
that a well written Java application should handle and re- ception handling coding practices
cover from. Code that anticipates checked exceptions must
follow the catch or specify requirements in Java. The re- could have been avoided by checking the proper condi-
quirements are as follows. The code should enclosed by a tions. Generally, unchecked exceptions should not be
try statement and must be followed by exception handlers. caught, although there are exceptions to this practice.
If an exception occurs in the try block, the exception will be
handled by the exception handlers. The try block must be • Not preserving original exception (NPOE): In-
followed by either one or more catch blocks, a finally block stead of handling exceptions at the lower level, devel-
or a combination of both to handle exceptions. If a method opers use the throw statement to throw exceptions
throws one or more exceptions it must list those exceptions to the higher level in response to another exception.
using a throws clause in its declaration. The second kind If they forget to wrap the original exception object
of exception is the error. The causes of these exceptions within the new exception object, they are throwing the
are external to the applications and the applications cannot exception but losing the original source of the problem.
recover from these exceptions. These are identified by Er-
• Use generic exception handler (UGEH): Instead
ror and its subclasses. The last kind of exception are those
of catching specific exceptions developers use a single
exceptions that are internal to an application and that the
catch block to collect all exceptions. As a result it
application cannot recover from it. These exceptions are not
may be difficult to determine why the exception was
subject to catch or specify requirements. They are indicated
thrown. consequently the runtime system cannot at-
by the Runtime exception and its subclasses. Exceptions
tempt recovery.
that are not indicated by Error, RuntimeException or
their subclasses, are checked exceptions. • Catching Error (CE): Applications cannot recover
from errors that are caused by the environment in
4. HOW DEVELOPERS USE EXCEPTION which the application is running. All errors in Java
are of type java.lang.Error. Thus, developers should
HANDLING IN JAVA not catch exceptions indicated by Error or its sub-
This section answers our research questions regarding ex- classes.
ception handling in Java. In addition to discussing the incor-
rect use of exception handling we also investigate its relation We are interested in finding how frequent the improper ex-
to developer experience, when new exception handling fea- ception handling coding practices are in source code repos-
tures are used, patterns of use in exception chaining and also itories. Figure 2 shows the frequency of those coding prac-
how developers create their own exception classes. tices in the GitHub data set. The figure shows that the data
set contains a significant number of all five bad coding prac-
4.1 Exception Handling Coding Practices to tices. We find that using an generic exception handler is the
Avoid most frequent one. This is an indication that developers are
To identify improper exception handling coding practices very reluctant in using exception handling. Next is ignor-
we review Java Language Specifications, previous research ing exceptions. Many developers are not aware of how to
papers [6, 4], software information sites, developer blogs and recover from exceptions or do not care to and thus leave the
books [5]. We identify the following coding patterns that catch and finally blocks empty. This could cause difficul-
need to be avoided. While this may not be a complete list, ties when maintaining applications as well as leave potential
they do represent the majority of the improper exception bugs in the code. We found a very small number of code
handling coding practices. examples where developers catch an error, indicating that
most developers are aware of this bad practice.
• Ignoring exceptions (IE): In this case developers
leave the catch or finally block empty. This defeats the 4.2 How developers use exception chaining
purpose of exceptions because this prevents programs In many applications lower level methods are required to
from recovering from exceptions. propagate information regarding exceptions to a higher level.
This enables applications to notify end users about excep-
• Catching unchecked exception (CUE): Unchecked tions and users can take appropriate actions at the more
exceptions are the result of programming errors that abstract level. Exception chaining is the mechanism that

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

to total exception handling examples


100
AutoCloseable Proficient
0.3 Expert
Files

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

own exceptions, we found that developers typically follow


100
the official Java language guidelines. We also investigated
the new exception handling features of Java and found that
50
developers use new exception handling features prior to their
0
official support in a Java release. The programs and addi-
2009 2010 2011 2012 2013 2014 2015 tional study results can be found online2 .
First Uses, by File

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

View publication stats

You might also like