JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

Certification Kits
SCJP 1.5 Exam Questions
SCJP 1.6 Exam Questions
SCWCD 5.0 Exam Questions
SCBCD 5.0 Exam Questions
SCJP 1.5 Links
SCJP 1.5 Home
SCJP Books
SCWCD Books
SCBCD Books
New to SCJP?
SCJP 1.4 or SCJP 1.5?
Objectives
Articles
Mock Exams
Tutorials
Forums
SCJP Exam Voucher
Certification Links
SCJP 1.4
SCJP 1.5
SCJP 1.6
SCWCD 1.4
SCWCD 5.0
SCBCD 5.0
SCEA
SCEA 5.0
JavaBeat
Home
Articles
Tips
Code
QnA
Forums
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
1. Assertion basics

2. Assertions vs. other error code

3. Working with assertions

4. Compiling with assertions

5. Controlling assertions programmatically

6. Rethrowing the assertion failure

7. Assertion examples

8. Knowing when to use assertions

Assertion in SCJP 1.4 & SCJP 5.0

The assertion facility provides a mechanism for adding optional “sanity checks” to your code. These checks are used during the development and testing phases, but are turned off when the software is deployed. This allows the programmer to insert debugging checks that might be too slow or memory-intensive to use in a real con-text, but that help during development. In a sense, assertions are a lot like error checks, except that they are turned off for deployment. Assertions generally are implemented in such a way that they can be compiled out; in languages like C and C++, this means using the preprocessor. Since Java doesn’t have macro facilities, features that otherwise might be created by the pro-grammer must be built into the language themselves. As a result, assertions have not been used widely in Java. The 1.4 release of the JDK corrects this. One of the most important features of this facility is that these checks can be turned on and off at runtime, which means that you don’t have to decide during development whether or not these checks should remain in the code or be removed before deployment.

Assertion basics

An assertion is a conditional expression that should evaluate to true if and only if your code is working correctly. If the expression evaluates to false, an error is sig-naled. Here is an example of an assertion (shown in bold):


public class aClass {
public void aMethod( int argument ) {
Foo foo = null;
// ... somehow get a Foo object
// Now check to make sure we've managed to get one:
assert foo != null;
}
}

This asserts that foo is not null. If foo is in fact null, an AssertionError is thrown. Any code that executes after this line can safely assume that foo is not null. Assertions are very simple, but we’ll be looking at their usage in detail because assertions are very important in the quest for robust code. In this chapter, we’ll learn not only how to use assertions, but when and where to use them.

Why use assertions?

It is widely acknowledged in programming circles that software isn’t stable enough. We all know we’re not doing enough error-checking. It is also acknowledged that error-handling comprises a substantial amount of programming effort and a sub-stantial portion of the resulting code. Error-handling code is also relatively dull to write, especially compared with the main algorithm whose errors are being handled. Furthermore, dealing with errors can sometimes force you to consider design questions that you may be trying to avoid. In such situations, programmers gener-ally just ignore the possibility of error, mostly because they don’t want to lose their train of thought.

As programmers, we need to do more error-checking, and assertions are an important step in this direction. Assertions can be used to catch conditions that we don’t expect to happen. This may sound paradoxical, but given that we rarely check for enough errors, it makes a certain sense. For every error we think of while pro-gramming, there are a whole bunch more that never occur to us. It’s not possible to eliminate all errors, but we can plan ahead for the unexpected.

1. Assertion basics

2. Assertions vs. other error code

3. Working with assertions

4. Compiling with assertions

5. Controlling assertions programmatically

6. Rethrowing the assertion failure

7. Assertion examples

8. Knowing when to use assertions

350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat