Exceptions and assertions CSE 331 University of Washington.

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.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
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.
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.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Exceptions and Assertions (Slides by Mike Ernst and David Notkin) 1.
CS203 Java Object Oriented Programming Errors and Exception Handling.
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Class Design: Handling Errors Reading: 2 nd Ed: Chapter 15 3 rd Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 10 Assertions & Exceptions.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
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.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
MIT AITI 2003 Lecture14 Exceptions
CSE 374 Programming Concepts & Tools
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Design by Contract Fall 2016 Version.
SWE 332 Last Modified Spring 2010 Paul Ammann
Chapter 12 Exception Handling
Reasoning About ADTs, Assertions and Exceptions
CSE331 SU Final Simple Summary
Exception Handling.
Presentation transcript:

Exceptions and assertions CSE 331 University of Washington

Failure causes Partial failure is inevitable Goal: prevent complete failure Structure your code to be reliable and understandable Some failure causes: 1. Misuse of your code Precondition violation 2. Errors in your code Bugs, representation exposure, many more 3. Unpredictable external problems Out of memory Missing file Memory corruption How would you categorize these? Failure of a subcomponent No return value (e.g., list element not found, division by zero)

Avoiding errors A precondition prohibits misuse of your code Adding a precondition weakens the spec This ducks the problem Does not address errors in your own code Does not help others who are misusing your code Removing the precondition requires specifying the behavior Strengthens the spec Example: specify that an exception is thrown

Defensive programming Check precondition postcondition representation invariant other properties that you know to be true Check statically via reasoning (& tools) Check dynamically at run time via assertions assert index >= 0; assert size % 2 == 0 : “Bad size for ” + toString(); Write the assertions as you write the code

When not to use assertions Don’t clutter the code x = y + 1; assert x == y + 1; Don’t perform side effects // useless, distracting assert list.remove(x); // modifies behavior if disabled // Better: boolean found = list.remove(x); assert found; How can you test at run time whether assertions are enabled? Why would you want to do this? Turn them off in rare circumstances (e.g., production code) “ java -ea ” runs Java with assertions enabled “ java ” runs Java with assertions disabled (default) Most assertions should always be enabled

What to do when something goes wrong Something goes wrong: an assertion fails Or if an assertion had been there, it would have failed Goal 1: Give information about the problem To the programmer A good error message is key! To the client code Goal 2: Prevent harm from occurring Abort: inform a human Perform cleanup actions, log the error, etc. Re-try Problem might be transient Skip a subcomputation Permit rest of program to continue Fix the problem (usually infeasible) External problem: no hope; just be informative Internal problem: if you can fix, you can prevent

Square root without exceptions // requires: x ≥ 0 // returns: approximation to square root of x public double sqrt(double x) { }

Square root with assertion // requires: x ≥ 0 // returns: approximation to square root of x public double sqrt(double x) { double result;... // compute result assert (Math.abs(result*result - x) <.0001); return result; }

Square root, specified for all inputs // throws: IllegalArgumentException if x < 0 // returns: approximation to square root of x public double sqrt(double x) throws IllegalArgumentException { if (x < 0) throw new IllegalArgumentException(); } Client code: try { y = sqrt(-1); } catch (IllegalArgumentException e) { e.printStackTrace(); // or take some other action } Caught by catch associated with nearest dynamically enclosing try Top-level default handler: stack trace, program terminates

Propagating an exception // returns: x such that ax^2 + bx + c = 0 // throws: IllegalArgumentException if no real soln exists double solveQuad(double a, double b, double c) throws IllegalArgumentException { // No need to catch exception thrown by sqrt return (-b + sqrt(b*b - 4*a*c)) / (2*a); } How can clients know whether a set of arguments to solveQuad is illegal?

Exception translation // returns: x such that ax^2 + bx + c = 0 // throws: NotRealException if no real solution exists double solveQuad(double a, double b, double c) throws NotRealException { try { return (-b + sqrt(b*b - 4*a*c)) / (2*a); } catch (IllegalArgumentException e) { throw new NotRealException(); } } class NotRealException extends Exception { NotRealException() { super(); } NotRealException(String message) { super(message); } NotRealException(Throwable cause) { super(cause); } NotRealException(String msg, Throwable c) { super(msg, c); } } Exception chaining: throw new NotRealException(e);

Exceptions as non-local control flow void compile() { try { parse(); typecheck(); optimize(); generate(): } catch (RuntimeException e) { Logger.log(“Failed: ” + e.getMessage()); } }

Informing the client of a problem Special value null - Map.get -1 - indexOf NaN - sqrt of negative number Problems with using special value Hard to distinguish from real results Error-prone: what if the programmer forgets to check result? The value should not be legal - should cause a failure later Ugly Less efficient A better solution: exceptions

Two distinct uses of exceptions Failures Unexpected Should be rare with well-written client and library Can be the client’s fault or the library’s Usually unrecoverable Special results Expected Unpredictable or unpreventable by client

Handling exceptions Failures Usually can’t recover If the condition is not checked, the exception propagates up the stack The top-level handler prints the stack trace Special results Take special action and continue computing Should always check for this condition Should handle locally

Why catch exceptions locally? Failure to catch exceptions violates modularity Call chain: A → IntegerSet.insert → IntegerList.insert IntegerList.insert throws an exception Implementer of IntegerSet.insert knows how list is being used Implementer of A may not even know that IntegerList exists Procedure on the stack may think that it is handling an exception raised by a different call Better alternative: catch it and throw it again “ chaining” or “translation” Do this even if the exception is better handled up a level Makes it clear to reader of code that it was not an omission

Java exceptions for failures and for special cases Checked exceptions for special cases Library: must declare in signature Client: must either catch or declare Even if you can prove it will never happen at run time There is guaranteed to be a dynamically enclosing catch Unchecked exceptions for failures Library: no need to declare Client: no need to catch Throwable RuntimeException and Error and their subclasses Exception … checked Error Runtime- exceptionsException

Avoid proliferation of checked exceptions Unchecked exceptions are better if clients will usually write code that ensures the exception will not happen There is a convenient and inexpensive way to avoid it The exception reflects unanticipatable failures Otherwise use a checked exception Must be caught and handled - prevents program defects Checked exceptions should be locally caught and handled Checked exceptions that propagate long distances suggests bad design (failure of modularity) Java sometimes uses null (or NaN, etc.) as a special value Acceptable if used judiciously, carefully specified Easy to forget to check

Exceptions in review Use an exception when Used in a broad or unpredictable context Checking the condition is feasible Use a precondition when Checking would be prohibitive E.g., requiring that a list be sorted Used in a narrow context in which calls can be checked Avoid preconditions because Caller may violate precondition Program can fail in an uninformative or dangerous way Want program to fail as early as possible How do preconditions and exceptions differ, for the client?

Exceptions in review, continued Use checked exceptions most of the time Handle exceptions sooner rather than later Not all exceptions are errors A program structuring mechanism with non-local jumps Used for exceptional (unpredictable) circumstances Also see Bloch’s Effective Java