This commit is contained in:
Daniel Bulant 2025-11-27 23:55:35 +01:00
parent 28ec01ea9f
commit 21fe12aa34
3 changed files with 56 additions and 46 deletions

View file

@ -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;
}
}

View file

@ -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());
}
}
}

View file

@ -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