in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 22 23 24 25 26 27 < 28 > 29 30 31 32 33 34 .. 166 >> Next

I am in the American League!
I am in the National League!
Notice that it didn't throw an AssertionError when I passed in the "IL" abbreviation. This is because I didn't enable assertions. The requirement to enable assertions is so that your code will not have any performance defect by having the assertions within the code when you choose not to use them. When assertions are not switched on, they are effectively the same as empty statements.
C:\pitfallsBook\#7>java -ea -cp . 2
I am in the American League!
Exception in thread "main” java.lang.AssertionError
at org.javapitfalls.item7.AssertionExample.printLeague(Unknown 2
at org.javapitfalls.item7.AssertionExample.main(Unknown Source)
Now the assertion is thrown, but it is not very descriptive as to which assertion was the cause of the problem. In this simple example, this is not a problem; it is still very easy to tell which assertion caused the problem. However, with many assertions (as is good practice), we need to better distinguish between assertions. So if we change line 20 to read
assert false : "What baseball league are you playing in?”;
this will give us a more descriptive response:
C:\pitfallsBook\#7>java -ea -cp . org.javapitfalls.item7.AssertionExample I am in the American League!
My Assertions Are Not Gratuitous! 63
Exception in thread "main" java.lang.AssertionError: What baseball 2
league are you playing in?
at org.javapitfalls.item7.AssertionExample.printLeague(Unknown 2
at org.javapitfalls.item7.AssertionExample.main(Unknown Source)
An important thing to note is that printLeague() is declared private. While the example was meant to show assertions to mark unreachable conditions, it was declared private in order to avoid confusion about an important point: You should never use assertions to do precondition checks for public methods. When assertions are turned off, you will not receive the benefit of any precondition checking. Furthermore, the AssertionError is a relatively simple methodology for returning errors, and more sophisticated mechanisms like typed exceptions would serve the cause better.
In Listing 7.2, we show how to use assertions to check postconditions.
01 package org.javapitfalls.item7;
03 public class AnotherAssertionExample {
05 private double taxRate;
07 public AnotherAssertionExample(double tax) {
09 taxRate = tax;
11 }
13 private double returnMoney (double salary) {
15 double originalSalary = salary;
17 if (salary > 10000) salary = salary * (1 - taxRate);
19 if (salary > 25000) salary = salary * (1 - taxRate);
21 if (salary > 50000) salary = salary * (1 - taxRate);
23 assert salary > 0 : "They can't take more than you have?";
25 assert salary <= originalSalary : "You can't come out ahead!";
27 return salary;
29 }
31 public static void main (String [] args) {
33 AnotherAssertionExample myExample = new 2
34 System.out.println("Tax Rate of 30%\n");
35 System.out.println("Salary of 5000:"+myExample.returnMoney(5000) ); 2
36 System.out.println("Salary of
37 System.out.println("Salary of 2
38 System.out.println("Salary of 2
39 // System.out.println("Salary of 2
Listing 7.2 (continued)
64 Item 7
41: myExample = new AnotherAssertionExample(-.3);
42: System.out.println("\n\nTax Rate of -30%\n”);
43: System.out.println("Salary of 5000:”+myExample.returnMoney(5000)); 44: System.out.println("Salary of 2
45: System.out.println("Salary of 2
47: }
49: }
Listing 7.2 (continued)
This shows how to check postconditions with an assertion. Here is the output from this example:
C:\pitfallsBook\#7>java -ea -cp . org.javapitfalls.item7.AnotherAssertionExample
Tax Rate of 30%
Salary of 5000:5000.0 Salary of 24000:16800.0 Salary of 35000:24500.0 Salary of 75000:36750.0
Tax Rate of -30%
Salary of 5000:5000.0
Exception in thread "main” java.lang.AssertionError: You can’t come out ahead!
org.javapitfalls.item7.AnotherAssertionExample.returnMoney(Unknown Source) at org.javapitfalls.item7.AnotherAssertionExample.main(Unknown
You can see that there are two assertions about the postconditions. First, we assert that you cannot return with less than zero money, and then we assert that you cannot return with more money than you started. There are examples of how to break both assertions: a negative salary (commented out) and a negative tax rate. The first example is commented out, because execution stops after the assertion error, and we wanted to demonstrate that the second assertion would also work.
Now that we have covered the assertion basics, we will go over some other interesting things about assertions. First, you can enable and disable assertions as desired by specifying them in the switches to the JVM. Here are some more examples:
Previous << 1 .. 22 23 24 25 26 27 < 28 > 29 30 31 32 33 34 .. 166 >> Next