COS 260 DAY 20 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Advertisements

CS102--Object Oriented Programming
Comp 249 Programming Methodology
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.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Chapter 9 Exception Handling Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch Announcements l Project 6 now out. »Milestone due Oct. 24th »Final project.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
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.
Chapter 81 Exception Handling Chapter 8. 2 Objectives become familiar with the notion of exception handling learn Java syntax for exception handling learn.
Java Software Solutions Foundations of Program Design Sixth Edition
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Object Oriented Programming
Chapter 12: Exception Handling
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.
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Eighth Lecture Exception Handling in Java
Chapter 16 Exception Handling
Chapter 9 Exception Handling
Chapter 12 Exception Handling and Text IO
Exceptions C++ Interlude 3
Chapter 14: Exception Handling
Announcements/Reminders
Exception Handling Chapter 9.
Exception Handling Chapter 8 Basic Exception Handling
Exceptions & exception handling
Advanced Java Programming
Exceptions & exception handling
Exceptions & Error Handling
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 9 Exception Handling
CMSC 202 Exceptions 2nd Lecture.
Exceptions 10-May-19.
Exception Handling Chapter 8 Basic Exception Handling
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

COS 260 DAY 20 Tony Gauvin

Agenda Questions? Assignment 4 Corrected Assignment 5 posted 1 A, 2 B’s and 3 non submits Assignment 5 posted Due Nov 20 @ 9:30 AM Capstone proposals? Quiz 3 Nov 24 Online, Open book, open notes Chaps 7, 8 & 9 Exception Handling October 30 Proposal Due E-mailed to Tony in Blackboard November 13 Progress Report E-mailed to Tony in Blackboard December 4 December 15 @ 10 AM All Deliverables & Presentation Due

Final Countdown Dec 4 Nov 17 Dec 8 Nov 20 Dec 11 Nov 24 Dec 15 @ 10 AM Intro to recursion Dec 8 No class, I have medical appointment in Boston Dec 11 Assignment 6 Due More recursion Dec 15 @ 10 AM Quiz 4 Chap 10 & 11 Capstone projects due Capstone Presentations Nov 17 Exception handling Programming lab Nov 20 Streams, file I/O and networking Assignment 5 Due Nov 24 Quiz 3 Chaps. 7, 8 & 9 Dec 1

Change to syllabus There will be only 6 assignments instead of the 8 mentioned in syllabus Each assignments will be worth 6.5% for a total of 39% percent of total grade Pre-professional conduct will be worth 9% of total grade OLD Four quizzes @ 8% each 32% Capstone Project 20% Eight Assignments @ 5% each 40% Pre-professional Conduct (see Contract on Classroom Behavior) 8% 100% NEW Four quizzes @ 8% each 32% Capstone Project 20% SIX Assignments @ 6.5% each 39% Pre-professional Conduct (see Contract on Classroom Behavior) 9% 100%

New Assignment Grading Criteria Criteria 1 30 % Does the program compile with no errors or warnings? Criteria 2 50% Does the program run and produce the intended outputs for the intended inputs? Criteria 3 5% Is the Code properly Commented? Criteria 4 5% Does the Code adhere to proper style and conventions for Java? Criteria 5 10% Is the code an original creative work of the student? Using leveraged code without citation  0 Students submitting duplicate code as another student  0

Exception Handling Chapter 9

Objectives Describe the notion of exception handling React correctly when certain exceptions occur Use Java's exception-handling facilities effectively in classes and programs

Basic Exception Handling: Outline Exceptions in Java Predefined Exception Classes

Exceptions in Java An exception is an object Throwing an exception Signals the occurrence of unusual event during program execution Throwing an exception Creating the exception object Handling the exception Code that detects and deals with the exception

Exceptions in Java Consider a program to assure us of a sufficient supply of milk View possible solution, listing 9.1 class GotMilk Chapter 9 Code\GotMilk.java Sample screen output

Exceptions in Java Now we revise the program to use exception-handling View new version, listing 9.2 class ExceptionDemo Chapter 9 Code\ExceptionDemo.java Sample screen output 1 Sample screen output 2

Exceptions in Java Note try block Note catch block Contains code where something could possibly go wrong If it does go wrong, we throw an exception Note catch block When exception thrown, catch block begins execution Similar to method with parameter Parameter is the thrown object

Sample screen output with no exception Exceptions in Java Note flow of control when no exception is thrown View demo with no exception, listing 9.3 class ExceptionDemo Chapter 9 Code\ExceptionDemo2.java Sample screen output with no exception

Sample screen output when exception is thrown Exceptions in Java Note flow of control when exception IS thrown View demo with exception, listing 9.4 class ExceptionDemo Chapter 9 Code\ExceptionDemo3.java Sample screen output when exception is thrown

Exception Flow red when exception blue with no exception java code statements … try { more code.. if (something bad happens) throw exception more code… } catch (the exception) even more code….

Predefined Exception Classes Java has predefined exception classes within Java Class Library Can place method invocation in try block Follow with catch block for this type of exception Example classes BadStringOperationException ClassNotFoundException IOException NoSuchMethodException https://docs.oracle.com/javase/tutorial/essential/exceptions/ https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

Predefined Exception Classes Example code https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html

Defining Your Own Exception Classes Must be derived class of some predefined exception class Text uses classes derived from class Exception View sample class, listing 9.5 class DivideByZeroException extends Exception Chapter 9 Code\DivideByZeroException.java View demo program, listing 9.6 class DivideByZeroDemo Chapter 9 Code\DivideByZeroDemo.java

Defining Your Own Exception Classes Different runs of the program Sample screen output 3 Sample screen output 2 Sample screen output 1

Defining Your Own Exception Classes Note method getMessage defined in exception classes Returns string passed as argument to constructor If no actual parameter used, default message returned The type of an object is the name of the exception class

Defining Your Own Exception Classes Guidelines Use the Exception as the base class Define at least two constructors Default, no parameter With String parameter Start constructor definition with call to constructor of base class, using super Do not override inherited getMessage

More About Exception Classes: Outline Declaring Exceptions (Passing the Buck) Kinds of Exceptions Errors Multiple Throws and Catches The finally Block Rethrowing an Exception Case Study: A Line-Oriented Calculator

Declaring Exceptions Consider a method where code (may) throw an exception May want to handle immediately May want to delay until something else is done Method that does not catch an exception Notify programmers with throws clause Programmer then given responsibility to handle exception

Declaring Exceptions Note syntax for throws clause Note distinction Keyword throw used to throw exception Keyword throws used in method heading to declare an exception

Declaring Exceptions If a method throws exception and exception not caught inside the method Method ends immediately after exception thrown A throws clause in overriding method Can declare fewer exceptions than declared But not more View program example, listing 9.7 class DoDivision Chapter 9 Code\DoDivision.java

Kinds of Exceptions In most cases, exception is caught … In a catch block … or Be declared in throws clause But Java has exceptions you do not need to account for Categories of exceptions Checked exceptions Unchecked exceptions

Kinds of Exceptions Checked exception Unchecked exception Must be caught in catch block Or declared in throws clause Unchecked exception Also called run-time Need not be caught in catch block or declared in throws Exceptions that coding problems exist, should be fixed

Kinds of Exceptions Examples why unchecked exceptions to are thrown Attempt to use array index out of bounds Division by zero Uncaught runtime exception terminates program execution

Kinds of Exceptions Figure 9.1 Hierarchy of the predefined exception classes

Errors An error is an object of class Error Similar to an unchecked exception Need not catch or declare in throws clause Object of class Error generated when abnormal conditions occur Errors are more or less beyond your control Require change of program to resolve Java keyword assert throws error if false ex assert (true == false);

Multiple Throws and Catches A try block can throw any number of exceptions of different types Each catch block can catch exceptions of only one type Order of catch blocks matter View example program, listing 9.8 class TwoCatchesDemo Chapter 9 Code\TwoCatchesDemo.java View exception class used, listing 9.9 class NegativeNumberException Chapter 9 Code\NegativeNumberException.java

Multiple Throws and Catches Note multiple sample runs Sample screen output 1 Sample screen output 2 Sample screen output 2

Multiple Throws and Catches Exceptions can deal with invalid user input To handle an exception thrown by a method It does not matter where in the method the throw occurs Use of throw statement be should be reserved for cases where it is unavoidable Text suggests separate methods for throwing and catching of exceptions Nested try-catch blocks rarely useful

The finally Block Possible to add a finally block after sequence of catch blocks Code in finally block executed Whether or not execution thrown Whether or not required catch exists Code runs even when there is an uncaught exception

Rethrowing an Exception Legal to throw an exception within a catch block Possible to use contents of String parameter to throw same or different type exception

Case Study A Line-Oriented Calculator User will enter result = 100 + 5 Should do addition, subtraction, division, multiplication Will use line input/output User will enter Operation, space, number Calculator displays result result = 100 + 5 result = 105 / 5 result = 21

Case Study Proposed initial methods Method to reset value of result to zero Method to evaluate result of one operation Method doCalculation to perform series of operations Accessor method getResult: returns value of instance variable result Mutator method setResults: sets value of instance variable result

Case Study View exception class, listing 9.10 class UnknownOpException Chapter 9 Code\UnknownOpException.java View first version of calculator, listing 9.11 (code in book is wrong) class PreLimCalculator Chapter 9 Code\PrelimCalculator.java Sample screen output

Case Study Final version adds exception handling Ways to handle unknown operator Catch exception in method evaluate Let evaluate throw exception, catch exception in doCalculation Let evaluate, doCalculation both throw exception, catch in main Latter option chosen

Case Study View final version, listing 9.12 class Calculator Chapter 9 Code\Calculator.java View final version, listing 9.12 class Calculator Sample screen output

Graphics Supplement: Outline Exceptions in GUIs Programming Example: a JFrame GUI Using Exceptions

Exceptions in GUIs Not good practice to use throws clauses in the methods In JFrame GUI or applet, uncaught exception does not end the program However GUI may not cope correctly, user may NOT receive sufficient instructions Thus most important to handle all checked exceptions correctly

Programming Example A JFrame GUI using exceptions View GUI class, listing 9.13 class ColorDemo Chapter 9 Code\ColorDemo.java Note exception class, listing 9.14 class UnknownColorException Chapter 9 Code\UnknownColorException.java View driver program, listing 9.15 class ShowColorDemo Chapter 9 Code\ShowColorDemo.java Better example of Color Chooser Chapter 9 Code\ColorSamplePopup.java

Programming Example

Summary An exception is an object derived from class Exception Descendants of class Error behave like exceptions Exception handling allows design of normal cases separate from exceptional situations Two kinds of exceptions Checked and unchecked

Summary Exceptions can be thrown by Java statements Methods from class libraries Programmer use of throw statement Method that might throw but not catch an exception should use throws clause Exception is caught in catch block

Summary A try block followed by one or mor catch blocks More specific exception catch types should come first Every exception type has getMessage method usable to recover description of caught description Do not overuse exceptions