/**
* {@link <a href=
* "https://[Log in to view URL]"
* target="_blank">Salto tra blocchi :: try-catch-finally</a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
public static int divideByZero(int numerator, int denominator) {
// il blocco lancia -> ArithmeticException
// l'eccezione non viene gestita
// passa il flusso al metodo precedente -> executeDivide
return numerator / denominator;
}
/**
* @see NumberFormatException
*
*/
public static int executeDivide(int numerator, int denominator) {
int result = 0;
try {
result = divideByZero(numerator, denominator);
}
// l'eccezione non viene gestita
// passa il flusso al metodo precedente -> main
catch (NumberFormatException ex) {
System.out.println(" NumberFormatException");
}
return result;
}
/**
* @see Integer#parseInt(String)
* @see NumberFormatException
*
*/
public static void executeParser() {
try {
int number = Integer.parseInt("123xx");
System.out.println(number);
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException");
} finally {
System.out.println("esegue questo blocco dopo il blocco try o il blocco catch");
}
System.out.println("qui prosegue il flusso dopo un NumberFormatException gestito");
}
public static void main(String args[]) throws Exception {
// Unit test - salto tra blocchi :: try-catch
try {
System.out.println(executeDivide(1, 0));
} catch (ArithmeticException ex) {
ex.printStackTrace();
}
System.out.println("qui prosegue il flusso dopo un ArithmeticException gestito");
// Unit test - salto tra blocchi :: try-catch-finally
executeParser();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: