SWE 332 Last Modified Spring 2010 Paul Ammann

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.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Written by: Dr. JJ Shepherd
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.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions and Exception Handling Carl Alphonce CSE116.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
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.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
CSC 243 – Java Programming, Spring, 2014 March 2014 Week 6ish, Exceptions.
Exceptions and assertions CSE 331 University of Washington.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
Written by: Dr. JJ Shepherd
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
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.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Introduction to Exceptions in Java CS201, SW Development Methods.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
C++ Exceptions.
Generics, Exceptions and Undo Command
Chapter 13 Exception Handling
CS102 – Exceptions David Davenport Latest: May 2015
Exceptions C++ Interlude 3
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Advanced Programming Behnam Hatami Fall 2017.
this keyword this : A reference to the implicit parameter Syntax:
Exception Handling Chapter 9.
Advanced Java Programming
this keyword this : A reference to the implicit parameter Syntax:
ATS Application Programming: Java Programming
Java Programming Language
Chapter 12 Exception Handling
Exception Handling in Java
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Lecture 9: Exceptions in Java CS201j: Engineering Software
Effective Java, 3rd Edition Chapter 10: Exceptions
Web Design & Development Lecture 7
CSE 143 Java Exceptions 1/18/2019.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Java Exceptions Dan Fleck CS211.
Go to pollev.com/cse143.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Effective Java, Chapter 9: Exceptions
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Exceptions for safe programming.
Exception Handling.
CSC 243 – Java Programming, Fall, 2008
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

SWE 332 Last Modified Spring 2010 Paul Ammann Java Exceptions SWE 332 Last Modified Spring 2010 Paul Ammann

Rationale for Exceptions Preconditions document undefined behavior Undefined behavior  Partial specification In any implementation, something happens Should clients rely on undocumented behavior? What happens in next release? Bottom line: Preconditions are usually undesirable But sometimes unavoidable… Exception handling: Transform preconditions to defined behavior One of several possible mechanisms…

Transforming a Precondition to an Exceptional Postcondition Key: Postcondition defines behavior for inputs excluded by precondition public double sqrt (double x) // precondition: x >= 0 // postcondition: … // precondition: // postcondition: If x < 0 throw IllegalArgumentException // else … /** * @param x the square of the intended result * @return approximate square root of x… * @throws IllegalArgumentException if x < 0 */

Java Exception Mechanism Checked Exceptions Unchecked Exceptions Ammann 2008

Exceptions Unchecked: client does not need to explicitly write special code Checked: client has to pay attention to these. Must write special code blocks Many different types of exceptions are provided. You can provide your own. Ammann 2008

Custom Exceptions public MyFavoriteException extends Exception { super(); } public MyFavoriteException(String s) {super(s);} can add more like state at the time of throwing the exception methods that let the invalid state be printed and other useful information Ammann 2008

Throwing Exceptions Explicit throws throw new NullPointerException(“”); throw new NPE(“class, method”); System throws String [ ] a = {“1”,”2”,”3”,”4”,”5”}; print(a[-1]); System throws IndexOutOfBoundsException Passing through (goes up the call chain) keeps going till it finds a handler Ammann 2008

Catching Exceptions In a special code block called try-block try{ }catch(ExceptionType instance){ } Ammann 2008

Catching Exceptions Can have multiple catch blocks, with different execution logic for different exceptions raised Class hierarchy matters in catching! If you catch an exception of type Exception, all subtypes are also caught! catching Exception is dangerous! WHY? Ammann 2008

Remember! try-catch block should be as short as possible. catch the right type of exception: to be sure of what’s the right thing to do Ammann 2008