Problem With Exceptions

2

Good guys, I told you my problem. I am practicing the subject of the exceptions in java and I have changed some of the validations that I have to replace them with exceptions but I have a problem and I do not know how to implement a second exception. I show you the part of the code where I have the problem:

public static void main(String[] args) {
    boolean flag = false;
    float sueldo = 0;
    while (!flag) {
        try {
            System.out.println("Introduce el sueldo");
            sueldo = MyInput.readFloat();
            flag = true;

        } catch (NumberFormatException nfe) {
            System.out.println("Error. El valor introducido no es correcto. Intentelo de nuevo...");
        }
    }
}

Well, that part of the exception is well placed and well implemented. The idea is that in case the salary that is entered is correct (the value is numeric) another exception is thrown if the value of that variable is < 500. I have already created a new class called "SalaryException" that inherits from the Exception class but I do not know how to implement it in the code that I have shown you before

public class SalarioExcepcion extends Exception{

  private float sueldo;

  public SalarioExcepcion(float sueldo) {
    super();
    this.sueldo = sueldo;
}

public String getMensaje() {
    String mensaje = null;

    if (this.sueldo <= 500) {
        mensaje = "Error. El sueldo del empleado ha de ser como MINIMO 501 €";
    }

    return mensaje;
}

}

Any idea how I can do it if it is possible?

Thank you very much.

    
asked by Josemanuu 06.12.2017 в 20:09
source

2 answers

2

The exception class does not need code, it should be empty, just call the constructors of the parent (Exception). For example:

public class SalarioExcepcion extends Exception {
    public SalarioExcepcion() {
        super();
    }

    public SalarioExcepcion(String mensaje) {
        super(mensaje);
    }
}

And now that you have your personal exception created, you go back to the code in which you want it to occur, and you throw it with a throw if the condition is breached. Also, catch it in the try-catch.

public static void main(String[] args) {
    boolean flag = false;
    float sueldo = 0;
    while (!flag) {
        try {
            System.out.println("Introduce el sueldo");
            sueldo = MyInput.nextFloat();
        if (sueldo <= 500) {
            throw new SalarioExcepcion();
        }

        flag = true;

    } catch (NumberFormatException nfe) {
        System.out.println("Error. El valor introducido no es correcto. Intentelo de nuevo...");
    } catch (SalarioExcepcion se) {
        System.out.println("Error. El sueldo del empleado ha de ser como MINIMO 501 €");
    }
}

}

A small detail: In the description of the error put the floor must be at least € 501. This is not really true, because it is a float. Therefore, by saying that you want a value

answered by 06.12.2017 / 20:48
source
2

Greetings, your second exception must be thrown when you have already overcome the NumberFormatException problem, that is, since you have built your salary, so the SalaryException does not contain the logic of

answered by 06.12.2017 в 20:34