Collect several exceptions from a try / catch in Java

1

I have a try/catch in my code. Inside the try I have different validations, each one throws an exception type that in turn extends the "general exception" parent class:

GENERAL EXCEPTION

public abstract class GeneralException extends Exception {
    public GeneralException (String errorMessage) {
        super(errorMessage);
    }
    public abstract int getError();
}

EXAMPLE OF DAUGHTER EXCEPTION

public static final String ERROR = "String exception";

public IncorrectStrException(String errorMessage) {
    super(ERROR);
}

@Override
public int getError() {
    return ERROR;
}

This is the code where I want to do the printStackTrace of all the exceptions that are thrown and save them in a list. However, it only saves the first exception. Does this happen because the execution stops when it is launched? I am new with exceptions, I have been searching and I have not found anything.

try {
    if (validateNum() && validateStr() && validateDate() && validateName()) {
        generateDoc();
    }
} catch (GeneralException e) {
    e.printStackTrace();
    exceptionList.add(e);
}

EXAMPLE OF VALIDATEX ()

public boolean validateInt(XSSFCell cell) throws IncorrectIntException {
    boolean validation = false;
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        validation = true;
    }
    return validation;
}
    
asked by Macaroni 04.12.2018 в 15:15
source

3 answers

1

I understood that each catch includes only one exception. Therefore, my solution was to put try / catch in the functions that validate to have all the exceptions of each validate:

public boolean validateInt(XSSFCell cell) throws IncorrectIntException {
    boolean validation = false;
    try {
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        validation = true;
    }
    return validation;
    } catch (IncorrectIntException e) { 
        e.pintStackTrace();
        exceptionList.add(e);
    }
}

I also had to change this part of the code and validate one by one:

if (validateNum() && validateStr() && validateDate() && validateName()) {
    generateDoc();

Like this:

boolean validate = validateNum() ? true : false;
validate = validateStr() ? true : false;
...

if (validate) { generateDoc(); }

I did not understand the flow that was still java with respect to the try / catch, now I see how much I have left to learn ...

    
answered by 05.12.2018 / 08:00
source
1

Since the IncorrectStrException is more concrete than the GeneralException, you should put 2 catch blocks, in order from more concrete to more general.

It would be like this:

try {
    if (validateNum() && validateStr() && validateDate() && validateName()) {
        generateDoc();
    }
} catch (IncorrectStrException e) {
    e.printStackTrace();
    exceptionList.add(e);
} catch (GeneralException e) {
    e.printStackTrace();
    exceptionList.add(e);
}

This way, if an exception occurs, it will be checked first if it is of the IncorrectStrException type and if it is not, it will go by the GeneralException. If you just set the GeneralException catch, they will all enter that catch since the IncorrectStrException is the (more concrete) daughter of GeneralException (general).

    
answered by 04.12.2018 в 15:48
0

You could handle the exceptions in the following way:

public void funcionX() {
try {
    //Código a ejecutar
} catch (NumberFormatException e) {
    //Captura la excepción del formato de número...
} catch (IllegalArgumentException e) {
    //Captura la excepción de argumentos...
} catch(...)

}

    
answered by 04.12.2018 в 15:46