1 / 9

3.0 EXCEPTION HANDLING

3.0 EXCEPTION HANDLING. 3.2 Apply concept of assertions. Learning Outcome Subtopic 3.2. FP301 OBJECT ORIENTED PROGRAMMING. WHAT IS AN ASSERTION?. Assertion are a way to test certain assumptions about the logic of a program.

zuwena
Download Presentation

3.0 EXCEPTION HANDLING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3.0 EXCEPTION HANDLING 3.2 Apply concept of assertions

  2. Learning Outcome Subtopic 3.2

  3. FP301 OBJECT ORIENTED PROGRAMMING WHAT IS AN ASSERTION? • Assertion are a way to test certain assumptions about the logic of a program. • It is used to generally to test assumptions about local program logic inside a method and usually not to test that external expectations are met. • One important feature of assertions is that they can be removed entirely from the code when it runs. • This makes it possible for you to enable assertions during program development, but to not have the tests executed at runtime when the final product is delivered to a customer. • This is n important difference between assertions and exceptions.

  4. FP301 OBJECT ORIENTED PROGRAMMING Assertion • Syntax of an assertion is: assert <boolean_expression> ; assert <boolean_expression> : <detail_expression> ; • If<boolean_expression> evaluates false, then an AssertionErroris thrown. • The second argument is converted to a string and used as descriptive text in the AssertionError message.

  5. FP301 OBJECT ORIENTED PROGRAMMING Recommended Uses of Assertions Assertions use to document and verify the assumptions and internal logic of a single method: • Internal invariants • Control flow invariants • Postconditions and class invariants Inappropriate uses of Assertions • Do not use assertions to check the parameters of a public method. • Do not use methods in the assertion check that can cause side-effects.

  6. FP301 OBJECT ORIENTED PROGRAMMING Internal Invariants The problem is: 1 if (x > 0) { 2 System.out.println(“x more than 0”); 3 } else { 4 System.out.println(“x equals or less than 0”); 5 } The solution is: 1 if (x > 0) { 2 System.out.println(“x more than 0”); 3 } else { 4 assert ( x == 0 ); 5 System.out.println(“x equals to 0”); // unless x is negative 6 }

  7. FP301 OBJECT ORIENTED PROGRAMMING Control Flow Invariants For example: 1 switch (suit) { 2 case Suit.CLUBS: // ... 3 break; 4 case Suit.DIAMONDS: // ... 5 break; 6 case Suit.HEARTS: // ... 7 break; 8 case Suit.SPADES: // ... 9 break; 10 default: assert false : "Unknown playing card suit"; 11 break; 12 }

  8. FP301 OBJECT ORIENTED PROGRAMMING Postconditions and Class Invariants For example: 1 public Object pop() { 2 int size = this.getElementCount(); 3 if (size == 0) { 4 throw new RuntimeException("Attempt to pop from empty stack"); 5 } 6 7 Object result = /* code to retrieve the popped element */ ; 8 9 // test the postcondition 10 assert (this.getElementCount() == size - 1); 11 12 return result; 13 }

  9. Controlling Runtime Evaluations of Assertions • If assertion checking is disabled, the code runs as fast as if the check was never there. • Assertion checks are disabled by default. Enable assertions with the following commands: >java -enableassertionsMyProgram or: >java -ea MyProgram • Assertion checking can be controlled on class, package and package hierarchy bases, see docs/guide/language/assert.html

More Related