My problem is that I have a window JOptionPane.showInputDialog
that receives a data of type int
. I wanted to do a try-catch
to handle the exception NumberFormatException
if they put a letter instead of a number. The problem I have is that if you close or select cancel in showInputDialog
, it also treats it as a NumberFormatException
since a int
can not be null. How can I treat these exceptions separately?
Here is the code snippet:
do{
try{
calcular.pedir_precio_materiales(
Integer.parseInt(
JOptionPane.showInputDialog("¿Cual es el coste total de los materiales?")
)
);
}catch(NumberFormatException exc){
excepción_de_formato=true;
System.out.println("Valor incorrecto, intente de nuevo");
}
}while(excepción_de_formato);
PD: calcular.pedir_precio_materiales()
is a method of another class and excepción_de_formato
is a boolean
that I stated previously.