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

View File

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

View File

@ -16,58 +16,32 @@ public class R2D2 extends RobotBasics {
}
/**
* @see robot.interfaces.RobotInstructions
*/
public int[] think(int[] input) throws RobotException {
if(isPowerOn()){
return selectionSort(input);
}else{
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName()));
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
RobotException robotexception = 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.
* @param input Zahlen, die ausgegeben werden sollen.
* @return String oder null
* @throws RobotException
* @see robot.interfaces.RobotInstructions
*/
@Override
public String speak(int[] input) throws RobotException {
if (isPowerOn()) {
return ausgabe(input, ";");
} else {
this.exceptions = new ExceptionStorage( new RobotException(robotExceptions.ILLEGALSTATE, getName()));
throw new RobotException(robotExceptions.ILLEGALSTATE, getName());
RobotException robotexception = 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;
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.EMPTYARRAY, getName()));
throw new RobotException(robotExceptions.EMPTYARRAY, getName());
RobotException robotexception = 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)
.collect(Collectors.joining(delemiter + " "));
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
}
}
@ -115,15 +117,22 @@ public abstract class RobotBasics implements Robot {
* @return input int [ ] (sorted)
* @throws RobotException
*/
public int[] insertionSort(int[] input) throws RobotException{
if(checkArray(input)){
return new int[0];
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
}
public int[] insertionSort(int[] input) throws RobotException {
if (checkArray(input)) {
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{
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
this.exceptions = new ExceptionStorage(robotexception);
throw robotexception;
}
}
}
/**
* Sorts any given array of integers with the selection Sort algorithm.
@ -152,9 +161,12 @@ public abstract class RobotBasics implements Robot {
}
return input;
}else{
this.exceptions = new ExceptionStorage(new RobotException(robotExceptions.MAGICVALUE, getName()));
throw new RobotException(robotExceptions.MAGICVALUE, getName());
RobotException robotexception = 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{
robotExceptions currentType;
String name;
public RobotException(robotExceptions type, String name){
super(getMessage(type, name));
this.name = name;
this.currentType = type;
}
@ -18,5 +20,10 @@ public class RobotException extends Exception{
return message;
}
@Override
public String toString(){
return getMessage(this.currentType, this.name);
}
}