changes in Exceptionhandling
parent
073ef835ed
commit
ce8a12071c
|
@ -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);
|
||||
|
||||
|
|
|
@ -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,23 +31,20 @@ 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 {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,12 +117,19 @@ 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];
|
||||
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{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue