Unsicher, ob das Code richtig ist

morry329

Mitglied
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:

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);
    }
}
Alle Tipps bin ich sehr dankbar.
 
Warum probierst Du den Code nicht einfach einmal aus?

Aber wenn Du den Aufgabentext liest: Da wird doch explizit geschrieben, dass die Methode zwei Exceptions wirft. Du fängst aber nur eine Exception. Das sieht also nicht richtig aus.

Kannst Du denn sagen, was für eine Exception da noch geworfen werden kann? Was passiert denn bei einem wert p != 0?
 
Warum probierst Du den Code nicht einfach einmal aus?

Aber wenn Du den Aufgabentext liest: Da wird doch explizit geschrieben, dass die Methode zwei Exceptions wirft. Du fängst aber nur eine Exception. Das sieht also nicht richtig aus.

Kannst Du denn sagen, was für eine Exception da noch geworfen werden kann? Was passiert denn bei einem wert p != 0?
Ach, habe ich wohl versehen, dass die Methode zwei Exceptions wirft. Danke für die Prüfung - bei einem Wert p != 0 glaube ich, ArithmeticException eine gute Wahl sein kann.
 
So, ich habe den Code überarbeitet und ausgeführt

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 Methode darf nicht geändert werden
    private int erroneousMethod(int p){
        if(p == 0){
            throw new IllegalArgumentException();
        }

        int x = 0x01;
        return p / (x >> Math.abs(p));
    }

    //zwei Ausnahme hängen lassen
    public void catchExceptions(int passthrough){
        var obj = new CatchingExceptions();

        try{
            System.out.println("let's try catching");
            obj.erroneousMethod(passthrough);
        } catch (ArithmeticException ex){
            System.out.println("arithmetic exception");
            ex.printStackTrace();
        } catch (IllegalArgumentException exp){
            System.out.println("illegal argument exception");
            exp.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CatchingExceptions check = new CatchingExceptions();

        check.catchExceptions(0);
        check.catchExceptions(5);
        check.catchExceptions(-5);
    }
}

Und er ergab die folgende Ausgabe

let's try catching
illegal argument exception //p == 0
let's try catching
arithmetic exception //p geteilt von 0
let's try catching
arithmetic exception //ebenso


Ich bin immer noch sicher ob der Code die Ausgabestellung erfüllt, aber er wirft mind. 2 Ausnahme.
 

Zurück
Oben