License: Creative Commons Attribution 3.0 Unported license (CC BY 3.0)
When quoting this document, please refer to the following
DOI: 10.4230/OASIcs.SLATE.2014.111
URN: urn:nbn:de:0030-drops-45641
Go to the corresponding OASIcs Volume Portal

Oliveira e Silva, Miguel ; Francisco, Pedro G.

Contract-Java: Design by Contract in Java with Safe Error Handling

12.pdf (0.6 MB)


Design by Contract (DbC) is a programming methodology in which the meaning of program entities, such as methods and classes, is made explicit by the use of programming predicates named assertions. A false assertion is always a manifestation of an incorrect program.

This simple founding idea, when properly applied, give programmers a tool able to specify, test, debug, document programs, as well as a mechanism to construct a simple, safe and sane error handling mechanism. Nevertheless, although well adapted to object-oriented programming (and other popular techniques such as unit testing), DbC still has a very low practical acceptance and application. We believe that one of the main reasons for such is the lack of a proper support for it in many programming languages currently in use (such as Java). A complete support for DbC requires not only the ability to specify assertions; but also the necessity to distinguish different kinds of assertions, depending of what is being asserted; a proper integration in object-oriented programming; and, finally, a coherent connection with error handling mechanisms.

It is in this last requirement that existing tools that extend Java with DbC mechanisms completely fail to properly, and coherently, integrate DbC within Java programming. The dominant practices for systematically handling failures in programming languages are not DbC based, using instead a defensive programming approach, either by using normal languages mechanisms (as in programming language C) or by the use of typed exceptions in try/catch based exception mechanisms.

In this article, we will present and justify the requirements posed on programming languages for a complete support for DbC; On the context of the last presented requirement – error handling – defensive programming will be discussed and criticized; It will be showed that, unlike Eiffel's original DbC error handling, existing typed exceptions in try/catch based exception mechanisms are not well adapted to algorithmic abstraction provided by methods; Finally, a new DbC Java extension named Contract-Java will be presented and it will be showed that it is coherently integrated both with Java existing mechanisms and DbC. It will be presented an innovative Contract-Java extension to DbC that automatically generates debugging information for (non-rescued) contract failures, that we believe further enhances the DbC debugging capabilities.

BibTeX - Entry

  author =	{Miguel Oliveira e Silva and Pedro G. Francisco},
  title =	{{Contract-Java: Design by Contract in Java with Safe Error Handling}},
  booktitle =	{3rd Symposium on Languages, Applications and Technologies},
  pages =	{111--126},
  series =	{OpenAccess Series in Informatics (OASIcs)},
  ISBN =	{978-3-939897-68-2},
  ISSN =	{2190-6807},
  year =	{2014},
  volume =	{38},
  editor =	{Maria Jo{\~a}o Varanda Pereira and Jos{\'e} Paulo Leal and Alberto Sim{\~o}es},
  publisher =	{Schloss Dagstuhl--Leibniz-Zentrum fuer Informatik},
  address =	{Dagstuhl, Germany},
  URL =		{},
  URN =		{urn:nbn:de:0030-drops-45641},
  doi =		{10.4230/OASIcs.SLATE.2014.111},
  annote =	{Keywords: design by contract, defensive programming, exceptions, Java, contract-Java}

Keywords: design by contract, defensive programming, exceptions, Java, contract-Java
Collection: 3rd Symposium on Languages, Applications and Technologies
Issue Date: 2014
Date of publication: 18.06.2014

DROPS-Home | Fulltext Search | Imprint | Privacy Published by LZI