Exception Handling

main
Lukas Berens 2023-01-07 16:32:29 +01:00
parent d42c1ef95e
commit 2a4801c60c
4 changed files with 55 additions and 33 deletions

View File

@ -1,20 +1,13 @@
package tpe.exceptions; package tpe.exceptions;
import java.util.*;
import java.lang.Thread;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class RobotException extends Exception { public class RobotException extends Exception{
public class MagicValueException extends Throwable { String robotName;
throw new Exception("Die Zahl 42 kann nicht verarbeitet werden"); String errorMessage;
}
RobotException(String errorMessage, String robotName){
public RobotException(String string) { super( "Robotname-"+robotName+": " + errorMessage );
// TODO Auto-generated constructor stub
} }
}
public RobotException MagicValueException() throws {
}
}

View File

@ -1,13 +1,10 @@
package tpe.exceptions; package tpe.exceptions;
public class RobotIllegalStateException { public class RobotIllegalStateException extends RobotException{
public boolean illegalState(boolean powerSwitch) { String robotName;
if(powerSwitch=false) public RobotIllegalStateException(String errorMessage, String robotName) {
{ super(errorMessage, robotName);
return false; this.robotName = robotName;
}
else
return true;
} }
} }

View File

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

View File

@ -3,9 +3,11 @@ package tpe.exceptions.roboter;
import java.util.*; import java.util.*;
import tpe.exceptions.RobotException; import tpe.exceptions.RobotException;
import tpe.exceptions.RobotIllegalStateException;
import tpe.exceptions.RobotMagicValueException;
public class R2D2 implements Robot { public class R2D2 implements Robot {
RobotException robotexception; private RobotException lastException;
private String name; private String name;
private boolean powerSwitch; private boolean powerSwitch;
private int id; private int id;
@ -44,11 +46,14 @@ public class R2D2 implements Robot {
public boolean isPowerOn() { public boolean isPowerOn() {
return powerSwitch; return powerSwitch;
} }
public void setLastException(RobotException lastException) {
this.lastException = lastException;
}
@Override @Override
public RobotException getLastException() { public RobotException getLastException() {
// TODO Auto-generated method stub return lastException;
return null;
} }
@Override @Override
@ -65,12 +70,28 @@ public class R2D2 implements Robot {
return output; 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 @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
for(int i = 0; i < zahlen.length; i++) { if(!isPowerOn())
if(zahlen[i]==42) {
throw new RobotException.MagicValueException(); 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 // Iterate through zahlen from left to right
for (int i = 0; i < zahlen.length - 1; i++) { for (int i = 0; i < zahlen.length - 1; i++) {
// Set the index of the current smallest element to i // Set the index of the current smallest element to i
@ -87,6 +108,7 @@ public class R2D2 implements Robot {
zahlen[minIndex] = temp; zahlen[minIndex] = temp;
} }
return zahlen; return zahlen;
}
} }