From 2a4801c60c4576f53fba5c3f038878eea1c4fd90 Mon Sep 17 00:00:00 2001 From: 2120940 <2120940@stud.hs-mannheim.de> Date: Sat, 7 Jan 2023 16:32:29 +0100 Subject: [PATCH] Exception Handling --- Roboter/tpe/exceptions/RobotException.java | 29 ++++++--------- .../RobotIllegalStateException.java | 13 +++---- .../exceptions/RobotMagicValueException.java | 10 ++++++ Roboter/tpe/exceptions/roboter/R2D2.java | 36 +++++++++++++++---- 4 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 Roboter/tpe/exceptions/RobotMagicValueException.java diff --git a/Roboter/tpe/exceptions/RobotException.java b/Roboter/tpe/exceptions/RobotException.java index deee895..e4a02e3 100644 --- a/Roboter/tpe/exceptions/RobotException.java +++ b/Roboter/tpe/exceptions/RobotException.java @@ -1,20 +1,13 @@ package tpe.exceptions; -import java.util.*; -import java.lang.Thread; + @SuppressWarnings("serial") -public class RobotException extends Exception { - -public class MagicValueException extends Throwable { - throw new Exception("Die Zahl 42 kann nicht verarbeitet werden"); - } - -public RobotException(String string) { - // TODO Auto-generated constructor stub - } - -public RobotException MagicValueException() throws { - - -} - -} +public class RobotException extends Exception{ + + String robotName; + String errorMessage; + + RobotException(String errorMessage, String robotName){ + super( "Robotname-"+robotName+": " + errorMessage ); + + } +} \ No newline at end of file diff --git a/Roboter/tpe/exceptions/RobotIllegalStateException.java b/Roboter/tpe/exceptions/RobotIllegalStateException.java index 917278b..826d535 100644 --- a/Roboter/tpe/exceptions/RobotIllegalStateException.java +++ b/Roboter/tpe/exceptions/RobotIllegalStateException.java @@ -1,13 +1,10 @@ package tpe.exceptions; -public class RobotIllegalStateException { - public boolean illegalState(boolean powerSwitch) { - if(powerSwitch=false) - { - return false; - } - else - return true; +public class RobotIllegalStateException extends RobotException{ + String robotName; + public RobotIllegalStateException(String errorMessage, String robotName) { + super(errorMessage, robotName); + this.robotName = robotName; } } diff --git a/Roboter/tpe/exceptions/RobotMagicValueException.java b/Roboter/tpe/exceptions/RobotMagicValueException.java new file mode 100644 index 0000000..8832052 --- /dev/null +++ b/Roboter/tpe/exceptions/RobotMagicValueException.java @@ -0,0 +1,10 @@ +package tpe.exceptions; + +@SuppressWarnings("serial") +public class RobotMagicValueException extends RobotException { + String robotName; + public RobotMagicValueException(String errorMessage, String robotName) { + super(errorMessage, robotName); + this.robotName = robotName; + } +} diff --git a/Roboter/tpe/exceptions/roboter/R2D2.java b/Roboter/tpe/exceptions/roboter/R2D2.java index 73bd57e..cfc3609 100644 --- a/Roboter/tpe/exceptions/roboter/R2D2.java +++ b/Roboter/tpe/exceptions/roboter/R2D2.java @@ -3,9 +3,11 @@ package tpe.exceptions.roboter; import java.util.*; import tpe.exceptions.RobotException; +import tpe.exceptions.RobotIllegalStateException; +import tpe.exceptions.RobotMagicValueException; public class R2D2 implements Robot { - RobotException robotexception; + private RobotException lastException; private String name; private boolean powerSwitch; private int id; @@ -44,11 +46,14 @@ public class R2D2 implements Robot { public boolean isPowerOn() { return powerSwitch; } - + + public void setLastException(RobotException lastException) { + this.lastException = lastException; + } + @Override public RobotException getLastException() { - // TODO Auto-generated method stub - return null; + return lastException; } @Override @@ -65,12 +70,28 @@ public class R2D2 implements Robot { return output; } + + public boolean checkRobotMagicValueException(int[] zahlen) { + boolean error=false; + for(int i = 0; i < zahlen.length; i++) { + if(zahlen[i]==42) { + error=true; + } + } + return error; + } + @Override public int[] think(int[] zahlen) throws RobotException { - for(int i = 0; i < zahlen.length; i++) { - if(zahlen[i]==42) - throw new RobotException.MagicValueException(); + if(!isPowerOn()) + { + throw new RobotIllegalStateException("Der Roboter ist ausgeschaltet!", this.getName()); } + else if(checkRobotMagicValueException(zahlen)==true) + { + throw new RobotMagicValueException("Zahl 42 kann nicht verarbeitet werden!",this.getName()); + } + else { // Iterate through zahlen from left to right for (int i = 0; i < zahlen.length - 1; i++) { // Set the index of the current smallest element to i @@ -87,6 +108,7 @@ public class R2D2 implements Robot { zahlen[minIndex] = temp; } return zahlen; + } }