Java Programming Week 4 - University Of New Haven

Transcription

OutlineExceptionsFilesInteracting ClassesJava ProgrammingWeek 4Alice E. FischerFebruary 12 – 17, 2015Java Programming - L4. . .1/47Summary and Homework

OutlineExceptionsFilesInteracting ClassesExceptionsPurpose and DefinitionsManaging ExceptionsThrowable ObjectsFilesDeclaring a Stream and Opening a FileEnd-of-File Handling: ComLine.javaInteracting ClassesClass DesignNorthwoods SweetsSummary and HomeworkJava Programming - L4. . .2/47Summary and Homework

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkExceptionsPurpose and exception typesException control: throw, catch, finallyHandling Exception and returning to executionLearn these exception typesJava Programming - L4. . .3/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkPurpose and DefinitionsExceptions are. . .An exception isI An object that is created when an unmanageable errorhappens. It is away to handle unexpected emergencies.I The exception system moves control from the function thatdiscovers a problem back to a function that can correct theproblem.I The exception is thrown from its point of origin to theenclosing context, or beyond.I Exceptions allow for the separation of the normal logic of theprogram (written in a try clause) from error processing(written in catch clauses).I Java has many kinds of exceptions and you can create yourown exception classes.Java Programming - L4. . .4/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkPurpose and DefinitionsDealing with ExceptionsIExceptions can be caught or declared.IIf you catch an exception, you can write code that deals withthe issue. After that, control can return to normal processing.IAn exception that is never caught will terminate the program.IAny function that might throw an exception must eitherdeclare the exception or catch it.IA Java program will not compile unless you either declare thatan exception can happen or handle it where it is thrown.Java Programming - L4. . .5/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkPurpose and DefinitionsDefinitions: Throwable, ErrorThrowable is a class that defines the fundamental properties ofthings that can be “thrown”. There are two subclasses:IIIIAn Error indicates serious problems that a reasonableapplication should not try to catch. Most such errors areabnormal conditions.A method is not required to declare in its throws clause anysubclasses of Error that might be thrown during the executionof the method but not caught, since these errors are abnormalconditions that should never occur.Example: OutOfMemoryError: The JVM is unable to allocatethe required amount of dynamic memory.The ThreadDeath error, though “normal”, is also a subclassof Error because most applications should not try to catch it.Java Programming - L4. . .6/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkPurpose and DefinitionsDefinitions: Exception, RuntimeExceptionException is a class derived from Throwable that indicatesconditions that a reasonable application might want to catch.There are many subclasses:IRuntimeException and its subclasses are exceptions that canbe thrown during the normal operation of the Java VirtualMachine.IA method is NOT required to declare in its throws clause anysubclasses of RuntimeException. They are called uncheckedexceptions and are probably caused by bad logic or badprogramming and could happen any time in any function.Java Programming - L4. . .7/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkPurpose and DefinitionsSome Runtime ExceptionsA program is not required to declare these, but may wish to catchthem:IArithmeticException: Divide by zero and other such problems.INumberFormatException: A mismatch between the contentsof the input stream and the type of data that was expected.INullPointerException: An attempt to use a variable that has function to compute it.IThere is a toString() function to format the information forprinting.IThere are no get-functions and no set-functions. All actionsinvolving an Item’s data are done by the Item class.Java Programming - L4. . .44/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkNorthwoods SweetsThe Model: Cart, a collection classCart is a collection of data objects.IItems are created by Northwoods, which then calls add() toput the new Item in the cart.IIn this case, an ordinary array of Items is used to model theshopping cart. A better implementation would be to use anArrayList, so that a heavy shopper can not overflow thecapacity of the cart.IAn attempt to store eleven items in THIS cart will throw anArrayIndexOutOfBoundsException.IThis breaks the design rule: A class takes care of its ownemergencies.Java Programming - L4. . .45/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkJava SummaryWe have looked at the following concepts, packages and classestoday:IThrowable:IUnchecked exceptions, checked exceptions, throwsItry, catch, finally, throw, getMessage()IStack traces and System.exit(0)IFile, PrintWriter, Scanner, close())IScanner: next(), nextLine()IModel, View, Controller software architecture.Java Programming - L4. . .Exception, RuntimeException and Error46/47

OutlineExceptionsFilesInteracting ClassesSummary and HomeworkHomework This WeekIRead Chapter 9: Text I/O and Chapter 10: Thinking inObjects.IP4. FilesDetails are given df.Java Programming - L4. . .47/47

Java Programming Week 4 Alice E. Fischer February 12 { 17, 2015 Java Programming - L4. 1/47. Outline Exceptions Files Interacting ClassesSummary and Homework . Learn these exception types Java Programming - L4. 3/47. Outline Exceptions Files Interacting ClassesSummary and Homework Purpose and De nitions Exceptions are.