Go to pollev.com/cse143.

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.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Building Java Programs Appendix B
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
CSE 143 Lecture 4: testing and complexity reading:
16-Jun-15 Exceptions. 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 null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. 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 null reference.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Computer Science 209 Software Development Handing Errors and Creating Documentation.
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Today’s lecture Review of chapter 8 Go over examples.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
CSE 143 Lecture 14: testing.
EECE 310: Software Engineering
MIT AITI 2003 Lecture14 Exceptions
The Class ArrayLinearList
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Exceptions 10-Nov-18.
this keyword this : A reference to the implicit parameter Syntax:
SWE 332 Last Modified Spring 2010 Paul Ammann
this keyword this : A reference to the implicit parameter Syntax:
CSE 143 Lecture 4 ArrayList Reading: 10.1.
Defining New Types of Objects, part 3
slides created by Ethan Apter
Object initialization: constructors
Exception Handling and Reading / Writing Files
Chapter 14 A List ADT.
CSE 143 Lecture 2 Collections and ArrayIntList
slides created by Ethan Apter
CSE 143 Java Exceptions 1/18/2019.
CSE 143 Lecture 4 More ArrayIntList:
Java Exceptions Dan Fleck CS211.
CSE 143 Lecture 5 More ArrayIntList:
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
slides created by Marty Stepp
Building Java Programs
slides created by Marty Stepp
Exceptions 25-Apr-19.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Special instance methods: toString
Exceptions 22-Apr-19.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Building Java Programs
Exceptions 10-May-19.
CSE 143 Lecture 6 interfaces; eclipse; testing
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides adapted from Marty Stepp
slides created by Ethan Apter and Marty Stepp
Building Java Programs Appendix B
CSE 143 Lecture 21 Advanced List Implementation
Java Basics Exception Handling.
Exceptions 5-Jul-19.
slides created by Marty Stepp
Presentation transcript:

Go to pollev.com/cse143

pollev.com/cse143 Suppose we had the following method: ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(1); list2.add(2); list1 = list2; list1.add(3); list2.add(4); What is the state of the lists after these calls? list1: [1, 3] list2: [2, 4] list1: [2, 3] list2: [2, 4] list1: [2, 3, 4] list2: [2, 3, 4] list1: [1, 2, 3] list2: [4] list1: [1, 2, 3, 4] list2: [] Other

Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header: // Returns the element at the given index. // Precondition: 0 <= index < size() public int get(int index) { return elementData[index]; } Stating a precondition doesn't really "solve" the problem, but it at least documents our decision and warns the client what not to do. What if we want to actually enforce the precondition?

Throwing exceptions (4.4) throw new ExceptionType(); throw new ExceptionType("message"); Generates an exception that will crash the program, unless it has code to handle ("catch") the exception. Common exception types: ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException, IllegalArgumentException, IllegalStateException, IOException, NoSuchElementException, NullPointerException, RuntimeException, UnsupportedOperationException Why would anyone ever want a program to crash?

Postconditions postcondition: Something your method promises will be true at the end of its execution. Often documented as a comment on the method's header: // Precondition : size() < capacity // Postcondition: value is added at the end of the list public void add(int value) { elementData[size] = value; size++; } If your method states a postcondition, clients should be able to rely on that statement being true after they call the method.

this keyword this : A reference to the implicit parameter Syntax: (the object on which a method/constructor is called) Syntax: To refer to a field: this.field To call a method: this.method(parameters); To call a constructor this(parameters); from another constructor:

Thinking about testing If we wrote ArrayIntList and want to give it to others, we must make sure it works adequately well first. Some programs are written specifically to test other programs. We could write a client program to test our list. Its main method could construct several lists, add elements to them, call the various other methods, etc. We could run it and look at the output to see if it is correct. Sometimes called a unit test because it checks a small unit of software (one class). black box: Tests written without looking at the code being tested. white box: Tests written after looking at the code being tested.

Tips for testing You cannot test every possible input, parameter value, etc. Think of a limited set of tests likely to expose bugs. Think about boundary cases Positive; zero; negative numbers Right at the edge of an array or collection's size Think about empty cases and error cases 0, -1, null; an empty list or array test behavior in combination Maybe add usually works, but fails after you call remove Make multiple calls; maybe size fails the second time only