HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Programming Correctness, Assertions
 and Exception Handling"
Volume: 4 Issue: 2, p. 42

	

Listing 1: An Exceptions Class

/**
*AssertionException.java
* @author Qusay H. Mahmoud, [email protected]
*/
AssertionException.java public class AssertionException extends
RuntimeException { public AssertionException() {
super(); }; public AssertionException(String msg) {
super(msg); } } 

Listing 2: An Assert Method

/**
*Assert.java
* @author Qusay H. Mahmoud, [email protected]
*/
Assert.java public class Assert { public static boolean NDE-BUG
= true; public static boolean assert( boolean
expression) throws AssertionException { if
(NDEBUG && !expression) { throw new AssertionExcep-
tion(); } return true;
} }

Listing 3: Implementing the Assert Class

/**
*Test Assertion.java
* @author Qusay H. Mahmoud, [email protected]
*/
import java.io.*; public class TestAssertion { public static
void main(String argv[]) { BufferedReader is = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Please input a number: ");
System.out.flush(); String ans=null;
try { ans = is.readLine(); }
catch(IOException e1) { System.out.println("Error:
"+e1); } int n = Integer.parseInt(ans);
// Assert.NDEBUG = false; // Uncomment the above line
// to turn assertions off.
Assert.assert(n > 0); // Use n. }
}
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.