Generics, Exceptions and Undo Command

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exceptions and Exception Handling Carl Alphonce CSE116.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
COP INTERMEDIATE JAVA Exception Handling Serialization.
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.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 3 Introduction to Collections – Stacks Modified
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.
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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 8-Exception Handling/ Robust Programming.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
OOP Tirgul 7. What We’ll Be Seeing Today  Packages  Exceptions  Ex4 2.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Exceptions: When things go wrong
Chapter 10 – Exception Handling
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Introduction Exception handling Exception Handles errors
Creating and Modifying Text part 2
Chapter 12 Exception Handling and Text IO
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Exceptions C++ Interlude 3
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Handling Exceptions.
Exception Handling Chapter 9.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Advanced Java Programming
Exceptions Handling the unexpected
Stacks, Queues, and Deques
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 in Java
Exception Handling Chapter 9 Edited by JJ.
Throwing and catching exceptions
CSE 143 Java Exceptions 1/18/2019.
Exceptions 19-Feb-19.
Chapter 12: Exceptions and Advanced File I/O
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Review: libraries and packages
Exceptions 10-May-19.
Stacks, Queues, and Deques
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Exceptions Review Checked Vs. Unchecked Exceptions
Presentation transcript:

Generics, Exceptions and Undo Command (Three unrelated concepts)

Why use generics? When you write a collection (something to store data, such as a linked list, arraylist, table, etc), you want it to be usable with all kinds of data. You want it to be usable to store Strings, or usable to store integers. Usually this is done by letting the collection work with the Object class, which everything inherits. The problem is that every time you take out an element, you have to cast it from Object. Also, while you want your collection to be usable for different types, you also want to limit the collection to using one type at a time (so you don’t accidentally put a String into a list of integers). “Generics” are a way for us to use “placeholder” types. The user chooses what they are when they first use the code, but then we plug that type everywhere in our methods. Example: the add() method of ArrayList<String> takes a String as a parameter. The add() method of ArrayList<Integer> takes Integer as a parameter. Inside ArrayList, it is declared as ArrayList <E>, and the add method takes a parameter of type E

A Generic Stack A stack is a collection that only allows us to add to the end, and only remove from the end. It turns out, imposing these limitations on a collection is sometimes useful, because it determines how we interact with the contents. Class AStack uses generics. In the main method, we have 2 instances of AStack, one using type Integer and one String. Notice that the type of stack is determined in instantiation, so we cannot push an Integer into a stack with type is String. Like normal stack, AStack supports pop, push, and peek.

What are exceptions? Exceptions are a way for code to “bail out” when it encounters something unexpected. Usually, this results in the program crashing and printing out a trace of where the exception occurred. In Java, we can write code like “throw new Exception(“Some message”)”. “throw” is a keyword, and you have to throw something that inherits from Error or Exception (both of these implement “Throwable”). These classes are ways for us to package information about what went wrong. We can write our own exception classes to specify specific errors When we throw an exception, this tells Java to abort what it’s doing and get out of the program. However, we can write code that “catches” thrown exceptions. That is, it responds to an exception and prevents the program from crashing immediately. Although a lot of times, all we do when catching exceptions is log the error in some way and then crash anyway.

Exception Handling try-catch blocks are ways for us to handle exceptions. We tell Java that if any exceptions are thrown inside a block that we label “try”, Java should try to catch them with the “catch” block that follows. The catch block specifies what kind of exceptions it catches. If you want to do different things in response to different exceptions, you can have several catch blocks, one after the other. You can see that in the driver, the push statements are in a try-catch block. There are 2 catch blocks, the first one catches FullCollectionException and the second one catches all other exceptions.

Checked and unchecked exceptions Most exceptions are called “checked exceptions”. That means that if your method throws one, it has to declare that it throws it in its header, saying something like “throws SomeException”. Any method that calls a method that has a throws declaration must also have the same throws declaration, or else it must handle the exception by surrounding the method call in a try-catch block. Exceptions that inherit from RuntimeException are called “unchecked exceptions”. These are not required to be declared in the header. The idea is that checked exceptions are ones that a programmer thinks other programmers should be ready to deal with in some way if they want to use her/his code. Unchecked exceptions are ones that the programmer may think are unrealistic to deal with, perhaps because they result from the misuse of their code, or bad programming (NullPointerException is an unchecked exception). Things that inherit from Error also don’t have to be declared, but by convention, these are only things that should never happen (like the results of hardware failure). Do not inherit from Error. This code has a user-defined exception, FullCollectionException. It will be thrown when user try to push an item into a full stack. The code that throws the exception is in method push of AStack.

Undoable Command Class In the past, we have used command classes to execute method calls in their own threads. We can also use command classes to make method calls undoable. Like before, we wrap the method call inside the execute/run method of a command class. However, we add an undo() method. The undo() method should do the opposite of what the execution did, so it will differ depending on the method. Then we create a class to manage our command history. We tell this “undoer” every time we want to execute a command, and it remembers it. Then, when we want to undo the command, the undoer can do this for us. Look at the Push and Pop undoable commands, the HistoryUndoer, and UndoerDriver. Notice that we don’t really need undoable command classes for peek() and size() since those do not change the stack (they are read methods)