diff --git a/week13/Gearbox.java b/week13/Gearbox.java index 45a2fcb..ffe92b2 100644 --- a/week13/Gearbox.java +++ b/week13/Gearbox.java @@ -3,15 +3,20 @@ package week13; public class Gearbox { private int gear = 1; - public static class IllegalGearChangeException extends RuntimeException {} + public static class IllegalGearChangeException extends RuntimeException { + } void changeGear(int newGear) { - if(newGear == gear) return; - if(newGear != -1 && (newGear < 1 || newGear > 5)) throw new IllegalArgumentException(); + if (newGear == gear) + return; + if (newGear != -1 && (newGear < 1 || newGear > 5)) + throw new IllegalArgumentException(); // newGear is now either -1 or 1..=5 var gearToCheck = newGear; - if(gearToCheck == -1) gearToCheck++; - if(Math.abs(gear - newGear) != 1) throw new IllegalGearChangeException(); + if (gearToCheck == -1) + gearToCheck++; + if (Math.abs(gear - gearToCheck) != 1) + throw new IllegalGearChangeException(); gear = newGear; } } diff --git a/week13/Printer.java b/week13/Printer.java index cd51724..77e8db4 100644 --- a/week13/Printer.java +++ b/week13/Printer.java @@ -3,51 +3,56 @@ package week13; import common.StdRandom; public class Printer { - static class OutOfPaperException extends Exception { - @Override - public String getMessage() { - return "No Paper pack found in printer.\n" + - "Check that there's enough paper left for this print job, that the Paper pack is clean and that the paper pack chip is undamaged. "+ - "Please note that third-party Paper packs may not be supported. Check that the Paper pack is made for this model."; - } - } - static class OutOfTonerException extends Exception { - @Override - public String getMessage() { - return "Printing supply pack is empty or was not found.\n" + - "Check that an official Printing supply pack is correctly inserted in the printer, try re-seating if necessary. " + - "Check that Printing supply packs are inserted into the correct color coded slots. Check that your Printing resupply subscription is active. " + - "Please note that third-party Printing supply packs may not be supported. Check that the Paper pack is made for this model."; - } - } - static class PaperJamException extends Exception { - @Override - public String getMessage() { - return "lp0 on fire."; - } + static class OutOfPaperException extends Exception { + @Override + public String getMessage() { + return "No Paper pack found in printer.\n" + + "Check that there's enough paper left for this print job, that the Paper pack is clean and that the paper pack chip is undamaged. " + + + "Please note that third-party Paper packs may not be supported. Check that the Paper pack is made for this model."; } + } - private static Exception getExceptionFromErrorCode(int errorCode) { - return switch(errorCode / 10) { - case 0 -> new OutOfPaperException(); - case 1 -> new OutOfTonerException(); - case 2 -> new PaperJamException(); - default -> throw new IllegalArgumentException(); - }; + static class OutOfTonerException extends Exception { + @Override + public String getMessage() { + return "Printing supply pack is empty or was not found.\n" + + "Check that an official Printing supply pack is correctly inserted in the printer, try re-seating if necessary. " + + + "Check that Printing supply packs are inserted into the correct color coded slots. Check that your Printing resupply subscription is active. " + + + "Please note that third-party Printing supply packs may not be supported. Check that the Paper pack is made for this model."; } + } - public static void print() throws Exception { - var rnd = StdRandom.uniformInt(30); - var exception = getExceptionFromErrorCode(rnd); - throw exception; + static class PaperJamException extends Exception { + @Override + public String getMessage() { + return "lp0 on fire."; } + } - public static void main(String[] args) { - try { - print(); - System.out.println("Printed successfully, thanks for your patronage."); - } catch(Exception e) { - System.err.println(e.getMessage()); - } + private static Exception getExceptionFromErrorCode(int errorCode) { + return switch (errorCode / 10) { + case 0 -> new OutOfPaperException(); + case 1 -> new OutOfTonerException(); + case 2 -> new PaperJamException(); + default -> throw new IllegalArgumentException(); + }; + } + + public static void print() throws Exception { + var rnd = StdRandom.uniformInt(30); + var exception = getExceptionFromErrorCode(rnd); + throw exception; + } + + public static void main(String[] args) { + try { + print(); + System.out.println("Printed successfully, thanks for your patronage."); + } catch (Exception e) { + System.err.println(e.getMessage()); } + } } diff --git a/week13/doc.typ b/week13/doc.typ index 128c5f1..ee7d485 100644 --- a/week13/doc.typ +++ b/week13/doc.typ @@ -9,7 +9,7 @@ Explain - in your own words - what is an exception? An object containing information about why a semi-hidden branching happened, usually used for error states. -Thrown exceptions bubble up, as if a return was used, until there's a registered exception handler for a given code region, usually by try/catch. +Thrown exceptions bubble up the call stack, as if a return was used, until there's a registered exception handler for a given code region, usually by try/catch. == Exercise 13.05