Hallo zusammen,
wegen einer Aufgabe habe ich das folgende Code geschrieben, jedoch bin ich noch nicht sicher ob es richtig ist. Die Aufgabestellung und alles sind hier:
Alle Tipps bin ich sehr dankbar.
wegen einer Aufgabe habe ich das folgende Code geschrieben, jedoch bin ich noch nicht sicher ob es richtig ist. Die Aufgabestellung und alles sind hier:
Java:
package C;
import java.io.IOException;
/*
The method erroneousMethod() throws two different exceptions. One is raised, if the parameter p has the value 0, the other is raised in any other case (i.e. p != 0).
Your task is to implement the method catchExceptions(int passthrough) in such a way, that it catches the two exceptions when calling erroneousMethod(passthrough) and prints exception catched for both exception types to the console.
You should not change the implementation of erroneousMethod()!
*/
public class CatchingExceptions {
//diese erroneousMethod wird vom Lehrer eingefügt und darf nicht verändert werden laut Aufgabestellung.
private int erroneousMethod(int p){
if(p == 0){
throw new IllegalArgumentException();
}
int x = 0x01;
return p / (x >> Math.abs(p));
}
//ab hier habe ich die restliche Zeile selber geschrieben
public void catchExceptions(int passthrough){
var obj = new CatchingExceptions();
try{
System.out.println("let's try catching");
obj.erroneousMethod(passthrough);
} catch (IllegalArgumentException exp){
System.out.println("catched exp! ");
exp.printStackTrace();
}
}
public static void main(String[] args) {
var obj2 = new CatchingExceptions();
obj2.catchExceptions(0);
obj2.catchExceptions(5);
obj2.catchExceptions(-5);
}
}