Today’s lecture Review of chapter 8 Go over examples.

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.
Yoshi
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
COMP 121 Week 5: Exceptions and Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
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.
Files and Streams CS 21a Chapter 11 of Horstmann.
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.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Java Software Solutions Foundations of Program Design Sixth Edition
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
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 }
Lecture 2 Exception handling Advanced Java Programming 1 dr inż. Wojciech Bieniecki
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
1 Features of Java (2) CS 3331 Sections 4.5 and 4.6.
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.
Recitation 3 2D Arrays, Exceptions. 2D arrays 2D Arrays Many applications have multidimensional structures: ●Matrix operations ●Collection of lists ●Board.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Exceptions in the Java programming language J. W. Rider.
Tirgul 13 Exceptions 1.
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Advanced Java Programming
ATS Application Programming: Java Programming
Exception Handling and Reading / Writing Files
Exception Handling.
Lecture 9: Exceptions in Java CS201j: Engineering Software
Exception Handling in Java
Go to pollev.com/cse143.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Creating and Modifying Text part 3
Exceptions 10-May-19.
Java Basics Exception Handling.
Exception Handling.
Presentation transcript:

Today’s lecture Review of chapter 8 Go over examples

Interfaces Why would I use an interface? How do I create an interface? Use it? Interfaces are types. What does this mean? How are interfaces similar to abstract classes? How are interfaces different from abstract classes?

Examples of Interfaces java.lang.Comparable. one method: → return negative : "less than", zero : "equal", positive : "greater than". → gives us a consistent way to sort data. Example: The String class implements Comparable. ( compareTo is available on Strings) java.lang.Iterator. Three methods: public int compareTo(Object other); public boolean hasNext(); public Object next (); public void remove ();

Let’s go over the examples

Questions?

Today’s lecture Review of chapter 9 Go over examples

Exceptions: Specialized Control Flow When would I use an exception over an if statement? What is an exception? How do I use it? What are catch blocks? Multiple catch blocks? Finally block? Why? What does it mean to propagate an exception? Chapter 11 in the text

Kinds of Exceptions Checked Exceptions: we must annotate the method signature admitting that our code could cause an exception. You can list many unhandled exceptions this way for a single method. Unchecked Exceptions can also be unhandled, but don't require the throws annotation: no try-catch block used around the suspicious code, and no further annotations. Only Error, RuntimeException and their child classes are unchecked. (You may still list them in the throws clause, as we did above for NullPointerException ). Error examples: failing disk, not enough memory; program can't solve these. Runtime Exceptions: good indication of program bugs, not behaviors we expect in a 'correct' program; their causes should be fixed. public int lazyBum (…) throws FileNotFoundException {..} public int foo () throws EOFException, IOException, NullPointerException{..}

Creating Your Own Exception Classes Exception definitions are classes. Extend these classes to make your own specialized exceptions. use fields, write constructors to pass info around. public class MyException extends Exception { public int num; public String msg; public MyException (int num, String msg) { this.num = num; this.msg = msg; }

Using Your Own Exceptions create values by calling the constructor. MyException myExc = new MyException(5,"yo"); begin exception propagation with a throw statement: throw myExc; or create and throw, all at once: throw new MyException(6,"hiya!");

Let’s go over the exercises

Questions?