teaching machines

CS 145 Lecture 33 – Exceptions

November 19, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

TODO

Think About This

The president of B-Ware Technologies tells the VP of Engineering to implement feature X. The VP tells a level 99 manager to implement feature X. The level 99 manager tells a level 98 manager to implement feature X. The level 98 manager breaks feature X into subfeatures Y and Z. The level 98 manager tells a level 85 manager to implement feature Y and a level 57 manager to implement feature Z. The level 85 manager tells a level 37 team lead to implement feature Y. The level 57 manager tells a level -10 outside contractor to implement feature Z. The level 37 team lead asks a level 18 developer to implement feature Y. The level 18 developer goes to implement feature Y, but discovers it would break laws in 7 countries in which B-Ware sells its products.

What happens next?

Loop Until Fixed Pattern

isValid = false
while !isValid
  try
    data = attempt the dangerous
    isValid = true
  catch
    print "Uh oh! Let's try that again."

use data

Code

Eggception.java

package lecture1119;

public class Eggception extends RuntimeException {

}

Responsibility.java

package lecture1119;

public class Responsibility {
  public static void main(String[] args) {
//    try {
      a();
//    } catch (Eggception e) {
//      System.out.println("I caught it!");
//    }
  }

  public static void a() {
    b();
  }

  public static void b() {
//    try {
      c();
//    } catch (Eggception e) {
      // try to fix it
//    }
  }

  public static void c() {
//    try {
      d();
//    } catch (Eggception e) {
      // try to fix situation
//    }
  }

  public static void d() {
    throw new Eggception();
  }
}

Flashcards.java

package lecture1119;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class Flashcards {
  private static String getName() {
    // try to open a file with their name in it
    // if file exists, read name
    // if not, ask for name

    try {
      Scanner in = new Scanner(new File("config"));
      // file did exist
      String name = in.nextLine();
      in.close();
      return name;
    } catch (FileNotFoundException e) {
      // file didn't exist
      Scanner in = new Scanner(System.in);
      System.out.print("Hey, user. Who are you? ");
      String name = in.nextLine();
      
      try {
        PrintWriter out = new PrintWriter(new File("config"));
        out.println(name);
        out.close();
      } catch (FileNotFoundException ee) {
        System.err.println("Uh, " + name + ", I think your hard drive is bricked.");
      }
      
      return name;
    }
  }
  
  public static void main(String[] args) throws FileNotFoundException {
    System.out.println("Hi, " + getName() + "!");
    
    Random g = new Random();
    Scanner in = new Scanner(System.in);
    int nRight = 0;
    int i = 0;

    while (i < 5) {
      int a = g.nextInt(10);
      int b = g.nextInt(10);

      System.out.print(a + " * " + b + " = ");
      int guess = in.nextInt();

      if (guess == a * b) {
        ++nRight;
      }

      ++i;
    }

    System.out.println("You answered " + nRight + " correctly!");
  }
}

LoopUntilFixed.java

package lecture1119;

import java.util.Scanner;

public class LoopUntilFixed {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    boolean isOkay = false;
    int denominator = 0;
    
    while (!isOkay) {
      try {
        System.out.print("Your favorite denominator: ");
        denominator = in.nextInt();
        System.out.println(2 / denominator);
        isOkay = true;
      } catch (ArithmeticException e) {
      }
    }
    
  }
}

Haiku

Objects, exceptions
What happens when they marry?
Objections are raised!