1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 8 Exceptions and Assertions.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
The Java Assert Statement. 2 Assert A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates a particle’s.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Computer Science 340 Software Design & Testing Design By Contract.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Ranga Rodrigo. Class is central to object oriented programming.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Object Oriented Programming
Chapter 12: Exception Handling
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Introduction to Exception Handling and Defensive Programming.
Programming with Assertions © Allan C. Milne v
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
(c) University of Washington13-1 CSC 143 Java Specifications: Programming by Contract Reading: Ch. 5.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Eighth Lecture Exception Handling in Java
Chapter 13 Exception Handling
PZ11A Programming Language design and Implementation -4th Edition
Chapter 6 CS 3370 – C++ Functions.
Logger, Assert and Invariants
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Testing Recap Testing can find problems, but cannot prove your program works Since exhaustive testing is impossible, select test cases with maximum likelihood.
Programming in Java Assertion.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Focus of the Course Object-Oriented Software Development
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
CS-1020 and Exception Handling
Computer Science 340 Software Design & Testing
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Defensive Programming
Presentation transcript:

1 Assertions

2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments are statements about the state the program should be in but assertions can tell the run-time system what the programmer believes is going on –if these assumptions turn out not to be the case, the run-time system can stop processing Java assert uses exceptions as its mechanisms

3 Assertions (cont.) assertion checking can be switched on and off as required assertions can be used during the test phase for a product, and then be switched off for production deployment assertions may be seen as an extension to the safety features provided by the language –built-in checks ascertain the validity of primitive operations, say, array indexing, casts, etc. –programmer-written checks do the same at a higher level for user-defined abstractions, e.g., new data structures

4 Assertions (cont.) seeing checks as a safety system, you may not want to turn them off (completely) turning on checking only during development and testing is sometimes described as: "wearing a life jacket when close to shore, and throwing it overboard once you are in the middle of the ocean"

5 Java assert statement an assert statement can be placed anywhere you normally place an exutable statement assert has two forms: assert BooleanExpression assert BooleanExpression : expression if assertions are enabled, these statements behave almost similarly to the following Java statements: if (! BooleanExpression) throw new Java.lang.AssertionError (); if (! BooleanExpression) throw new Java.lang.AssertionError ("" + expression); Note. A Throwable expression is set as a cause.

6 Java assert (cont.) technically, AssertionError has constructors that turns expression values into a string –the original expression value becomes lost, and so cannot be (mis)used for further processing by Java compiler default, assertions are disabled at runtime; –command-line switches allow you to selectively enable or disable assertions –may selectively set assertion state for a class and its inner classes, and the same for packages programming environments often set assertion state on (as a more appropriate default value)

7 Assertion idioms test impossibilities (never happens): switch (a) { case 0: return "Red"; case 1: return "Green"; case 2: return "Blue"; default: assert false; } the compiler may sometimes deduce that code cannot be reached, and won't allow even an (unnecessary) assert there.

8 Assertion idioms (cont.) Algorithm Verification: slow but reliable method to determine the result, compared to the outcome of the efficient but complicated method public Data fastMethod (E [ ] array) { Data result; // efficient algorithm calculates result.. assert result.equals (slowMethod (array)); return result; }

9 Assertion idioms (cont.) Class invariant checking: true when the method starts, and the method ensures it will be true after the method finishes invariants relate to the private state of an object and they should be stated as assertions. public void put (E element) { assert Invariant (); elements [size++] = element; assert Invariant (); } public boolean Invariant () { return size >= 0..; // state valid and consistent }

10 Assertion idioms (cont.) Precondition checking: it is responsibility of the code calling the method to meet any preconditions the method has if preconditions are violated, in principle, all bets are off: no need to fulfill responsibilities however, to support early detection of errors and minimize damage, not assertions but exceptions are used to report the violations of preconditions public E getElement () { if (isEmpty ()) // not: assert throw new NoSuchElementException("empty"); return...; } precondition checks are used for public methods

11 Assertion idioms (cont.) Postcondition checking: may relate both to the return value from the method, and to the whole state of system public void putElement (E element) { //.. put it into container assert ! isEmpty (); } postcondition checks performed using asserts in any kind of method (public or private)

12 Summary practice defensive programming: check for the unexpected exceptions are be used to –return "alternative" results that presumable can be handled by the caller: checked exceptions –report run-time failures (memory exhaustion, invalid index argument, numerical overflow, etc.): unchecked exceptions throw exceptions for precondition errors asserts makes checks to reveal internal errors (bugs) prefer predefined standard exceptions remember to clean up/release resources write exception-safe components (failure atomicity)