1 / 13

CS 211 Interfaces, Exceptions

CS 211 Interfaces, Exceptions. CS 211 Interfaces, Exceptions. 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?

vanna
Télécharger la présentation

CS 211 Interfaces, Exceptions

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. CS 211Interfaces, Exceptions CS 211Interfaces, Exceptions

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

  3. 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?

  4. 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. (compareTois available on Strings) java.lang.Iterator. Three methods: public intcompareTo(Object other); public booleanhasNext(); public Object next (); public void remove ();

  5. Let’s go over the examples

  6. Questions?

  7. Today’s lecture • Review of chapter 9 • Go over examples

  8. 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

  9. 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 intlazyBum(…)throws FileNotFoundException{..}public int foo () throws EOFException, IOException, NullPointerException{..}

  10. 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 MyExceptionextends Exception{ public intnum; public String msg; public MyException (intnum, String msg) { this.num = num; this.msg = msg; } }

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

  12. Let’s go over the exercises

  13. Questions?

More Related