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;
import java.util.*;
import java.lang.Thread;
@SuppressWarnings("serial")
public class RobotException extends Exception {
public class RobotException extends Exception{
String robotName;
String errorMessage;
RobotException(String errorMessage, String robotName){
super( "Robotname-"+robotName+": " + errorMessage );
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 {
}
}

View File

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

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 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;
@ -45,10 +47,13 @@ public class R2D2 implements Robot {
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;
}
}