1 / 5

Generics, Exception and Undo Command

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 and String. Notice that the type of stack is determined in initiation, so we cannot push an Integer into a stack which type is string.

quasim
Download Presentation

Generics, Exception and Undo Command

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generics, Exception and Undo Command

  2. A Generic Stack • Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer and String. Notice that the type of stack is determined in initiation, so we cannot push an Integer into a stack which type is string. • Like normal stack, AStack supports pop, push, and peek. • There is a user-defined exception class, FullCollectionException. It will be thrown when user try to push item into a full stack. The code that throws the exception is in method push of AStack

  3. Exception Handling • You can see that in the main method, 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 exceptions. The advantage of using the try-catch block is that the program will not be terminated if an unexpected exception is caught.

  4. RuntimeException • Look at the code of Class FullCollectionException, you can see that it extends RuntimeException. Modify the code to let it extends Exception, what do you see? • Although RuntimeException is a subclass of Exception, they are actually belong to different categories. RuntimeException is unchecked exception, which means that the compiler does not require the method to catch the exception. In another word, we don’t need to have a try-catch block for RuntimeException or let a method declare to throw the exception. The opposite one is the checked exception, which we have to use a try-catch block or let a method declare to throw it.

  5. Undoable Command Class • The main method to run the undoable stack is in class AnUndoableStack. AnUndoable uses undoer to execute or undo commands. • Class AddCommand is an undoable command class. Basically, undoable command classes are just ordinary command classes that have one more method undo(); Undo() is just take the opposite effect of the execute(), so in different command class we need different undo(). • Since logically we can only undo once, so we need a flag to determine whether undo is called or not. • If we put all command in a list, then it is possible to undo a series of commands. It is implemented in class UndoerHistory.

More Related