changes in Exceptionhandling

main
Philipp3107 2022-12-25 19:57:20 +01:00
parent 073ef835ed
commit ce8a12071c
13 changed files with 62 additions and 70 deletions

View File

@ -28,10 +28,10 @@ public class Main {
System.out.print(" " + sorted[i]); System.out.print(" " + sorted[i]);
} }
}catch(RobotException re){ }catch(RobotException re){
System.out.println(re); re.printStackTrace();
} }
System.out.println("last exception thrown");
String re = Herb.getLastException().toString(); String re = Herb.getLastException().toString();
System.out.println(re); System.out.println(re);
@ -43,8 +43,9 @@ public class Main {
System.out.print(" " + sorted[i]); System.out.print(" " + sorted[i]);
} }
}catch(RobotException e){ }catch(RobotException e){
System.out.println(e); e.getLocalizedMessage();
} }
System.out.println("last exception thrown");
re = Herb.getLastException().toString(); re = Herb.getLastException().toString();
System.out.println(re); System.out.println(re);

View File

@ -1,5 +1,6 @@
package domain; package domain;
import robot.exceptions.ExceptionStorage;
import robot.exceptions.RobotException; import robot.exceptions.RobotException;
import robot.exceptions.robotExceptions; import robot.exceptions.robotExceptions;
@ -11,16 +12,16 @@ public class C3PO extends RobotBasics {
super(id, name); super(id, name);
} }
// public String ausgabe(int[] input) throws RobotException{ /* public String ausgabe(int[] input) throws RobotException{
// if(input.length != 0 && !checkArray(input)){ if(input.length != 0 && !checkArray(input)){
// return Arrays.stream(input) return Arrays.stream(input)
// .mapToObj(Integer::toString) .mapToObj(Integer::toString)
// .collect(Collectors.joining("; ")); .collect(Collectors.joining("; "));
// }else{ }else{
// throw new RobotException(robotExceptions.MAGICVALUE, getName()); throw new RobotException(robotExceptions.MAGICVALUE, getName());
// // throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42."); // throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
// } }
// } }*/
@Override @Override
public String speak(int[] input) throws RobotException { public String speak(int[] input) throws RobotException {
if(isPowerOn()){ if(isPowerOn()){
@ -30,23 +31,20 @@ public class C3PO extends RobotBasics {
return re.toString(); return re.toString();
} }
}else{ }else{
throw new RobotException(robotExceptions.ILLEGALSTATE, getName()); RobotException robotException = new RobotException(robotExceptions.ILLEGALSTATE, getName());
//throw new RobotIllegalStateException(getName() + " is turned off."); this.exceptions = new ExceptionStorage(robotException);
throw robotException;
} }
} }
public int[] sorting(int[] input) throws RobotException{ public int[] sorting(int[] input) throws RobotException{
if(input.length != 0 ){
if(checkArray(input)){ if(checkArray(input)){
return insertionSort(input); return insertionSort(input);
}else{ }else{
throw new RobotException(robotExceptions.MAGICVALUE, getName()); throw new RobotException(robotExceptions.MAGICVALUE, getName());
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42"); //throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");
} }
}
return new int[0];
} }
@Override @Override
public int[] think(int[] input) throws RobotException { public int[] think(int[] input) throws RobotException {

View File

@ -16,58 +16,32 @@ public class R2D2 extends RobotBasics {
} }
/**
* @see robot.interfaces.RobotInstructions
*/
public int[] think(int[] input) throws RobotException { public int[] think(int[] input) throws RobotException {
if(isPowerOn()){ if(isPowerOn()){
return selectionSort(input); return selectionSort(input);
}else{ }else{
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName())); RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
throw new RobotException(robotExceptions.ILLEGALSTATE, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
} }
/*
* this method sorts
* @param input
* @return integer array
* @throws RobotException
public int[] sorting(int[] input) throws RobotException {
if(checkArray(input)){
//Selectionsort
int small;
for (int i = 0; i <input.length - 1; i++) {
small = i;
for (int j = i + 1; j < input.length; j++) {
//if there is a smaller number on this position
if (input[j] < input[small]) {
small = j;
//swap values
int temp = input[i];
input[i] = input[small];
input[small] = temp;
}
}
}
return input;
}else{
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
}*/
/** /**
* Die Methode nimmt ein Array aus int entgegen und wandelt diese in einen String um. * @see robot.interfaces.RobotInstructions
* @param input Zahlen, die ausgegeben werden sollen.
* @return String oder null
* @throws RobotException
*/ */
@Override @Override
public String speak(int[] input) throws RobotException { public String speak(int[] input) throws RobotException {
if (isPowerOn()) { if (isPowerOn()) {
return ausgabe(input, ";"); return ausgabe(input, ";");
} else { } else {
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName())); RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
throw new RobotException(robotExceptions.ILLEGALSTATE, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
} }
} }

View File

@ -83,8 +83,9 @@ public abstract class RobotBasics implements Robot {
} }
return true; return true;
}else{ }else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.EMPTYARRAY, getName())); RobotException robotexception = new RobotException(robotExceptions.EMPTYARRAY, getName());
throw new RobotException(robotExceptions.EMPTYARRAY, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
@ -104,8 +105,9 @@ public abstract class RobotBasics implements Robot {
.mapToObj(Integer::toString) .mapToObj(Integer::toString)
.collect(Collectors.joining(delemiter + " ")); .collect(Collectors.joining(delemiter + " "));
}else{ }else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName())); RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
throw new RobotException(robotExceptions.MAGICVALUE, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
} }
@ -117,10 +119,17 @@ public abstract class RobotBasics implements Robot {
*/ */
public int[] insertionSort(int[] input) throws RobotException { public int[] insertionSort(int[] input) throws RobotException {
if (checkArray(input)) { if (checkArray(input)) {
return new int[0]; for (int i = 1; i < input.length; i++) {
int b = i - 1;
int key = input[i];
while (b >= 0 && input[b] > key) input[b + 1] = input[b--];
input[b + 1] = key;
}
return input;
}else{ }else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName())); RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
throw new RobotException(robotExceptions.MAGICVALUE, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
} }
@ -152,9 +161,12 @@ public abstract class RobotBasics implements Robot {
} }
return input; return input;
}else{ }else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName())); RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
throw new RobotException(robotExceptions.MAGICVALUE, getName()); this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
} }
} }
} }

View File

@ -2,8 +2,10 @@ package robot.exceptions;
public class RobotException extends Exception{ public class RobotException extends Exception{
robotExceptions currentType; robotExceptions currentType;
String name;
public RobotException(robotExceptions type, String name){ public RobotException(robotExceptions type, String name){
super(getMessage(type, name)); super(getMessage(type, name));
this.name = name;
this.currentType = type; this.currentType = type;
} }
@ -18,5 +20,10 @@ public class RobotException extends Exception{
return message; return message;
} }
@Override
public String toString(){
return getMessage(this.currentType, this.name);
}
} }