Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Bil Lewis

Dr. Dobb's Bloggers

Assertations vs. Exceptions

March 25, 2010

They should be distinct, but are often confused.The generally accepted defintion of assertaion is "A statement of something that MUST be true. If it is not, then your program is broken. You cannot recover from a broken program. Fix the bugs!"

An exception, by contrast, is thrown when a condition required for the correct functioning of a method isn't met. An exception may be caught and recovered from.

The concept we're interested in here is the Zen Nature of the condition we're testing. Exceptions and Errors in Java are merely implementation techniques. An assertion may well be implemented with exceptions, but its Zen Nature is not to be caught.

For example, should the user specify a non-existant file, our program may well throw a FileNotFoundException. Clearly we can catch it and ask the user for another file name.

By contrast, should the list of free pages plus the list of used pages not equal the list of all pages, we're screwed. Some fundamental method in our operating system did something wrong and we have no idea of what else might be wrong. So we can't recover.

The intention of assertions is to provide a security check during the writing and debugging of the program. By the time the program is released, no assertions should ever fail. Therefore assertions should be turned off in production.

This in turn implies that we don't care how expensive the assertion is. Excluding any real-time requirements, it is perfectly acceptable for our program to run 10x slower with assertions turned on.

A perfectly adaquate (if ugly) implementation of assertions would be:

if (ASSERTIONS_ENABLED && !condition()) throw new AssertionError("Bad condition");

Because ASSERTIONS_ENABLED would be a constant, the complier can skip the entire statement if it's false. The assertions in Java work much like this, with some really nice advantages.

A very common mistake many programmer make is to use assertions where exceptions should have been used. They then have to release their production code with assertions turned on. The obvious problems are that the code will be slow, it will be harder to read, and the new guy they hired to run the builds will turn them off.

Now, how do you use assertions?A very common mistake many programmer make is to use assertions where exceptions should have been used.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.