Tratto da: F. Sanpietro, O. Sanpietro – Java: il linguaggio e la tecnologia – TRAMONTANA
Dati n reali inseriti dall’utente calcolarne la media, con gestione delle possibili eccezioni
- ArrayIndexOutOfBoundsException
- NumberFormatException
- ArithmeticException
import javax.swing.JOptionPane; public class Eccezioni0 { public static void main(String args[]) { String input; double v[]=new double[5]; input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double media=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); media+=v[i]; } media/=numElementi; JOptionPane.showMessageDialog(null, "Media="+media); } }
Se numElementi > 5 si ha un accesso errato all'array v, catturiamolo!
import javax.swing.JOptionPane; public class Eccezioni1 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double media=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); media+=v[i]; } media/=numElementi; JOptionPane.showMessageDialog(null, "Media="+media); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } } }
Se input non rappresenta effettivamente un numero reale si ha un numero malformato, catturiamolo!
import javax.swing.JOptionPane;public class Eccezioni2 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double media=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); media+=v[i]; } media/=numElementi; JOptionPane.showMessageDialog(null, "Media="+media); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Input errato"); } } }
Se numElementi==0 si ha una divisione per zero che produce il risultato NaN, catturiamolo!
import javax.swing.JOptionPane; public class Eccezioni3 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double media=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); media+=v[i]; } if(numElementi==0) throw new ArithmeticException(); media/=numElementi; JOptionPane.showMessageDialog(null, "Media="+media); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Input errato"); } } }
Continua...
import javax.swing.JOptionPane; public class Eccezioni10 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double somma=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); somma+=v[i]; } JOptionPane.showMessageDialog(null, "Media=" + dividi(somma, numElementi)); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Input errato"); } } public static double dividi(double x, double y) { if(y==0) throw new ArithmeticException("Divisione per zero!"); else return x/y; } }
Nel metodo dividi() potrebbe essere lanciata un'eccezione
throw new ArithmeticException();
oppure
throw new ArithmeticException("Divisione per zero!");
che provocherebbe l'interruzione dell'esecuzione, è meglio delegare alla sua cattura il metodo chiamante
import javax.swing.JOptionPane; public class Eccezioni11 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double somma=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); somma+=v[i]; } JOptionPane.showMessageDialog(null, "Media="+dividi(somma, numElementi)); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Input errato"); } catch(ArithmeticException ex) // 3 { JOptionPane.showMessageDialog(null, "Attento: " + ex.getMessage()); } } public static double dividi(double x, double y) throws ArithmeticException // 2 { if(y==0) throw new ArithmeticException("Divisione per zero!"); // 1 else return x/y; } }
Osserva
- throw, lancia
- throws, rilancia al metodo chiamante
- catch, cattura
Continua...
import javax.swing.JOptionPane; public class Eccezioni12 { public static void main(String args[]) { String input; double v[]=new double[5]; try { input=JOptionPane.showInputDialog("Quanti elementi?"); int numElementi=Integer.parseInt(input); double somma=0; for(int i=0; i < numElementi; i++) { input=JOptionPane.showInputDialog("v["+i+"]="); v[i]=Double.parseDouble(input); somma+=v[i]; } JOptionPane.showMessageDialog(null, "Media=" + dividi(somma, numElementi)); } catch(ArrayIndexOutOfBoundsException ex) { JOptionPane.showMessageDialog(null, "Numero elementi eccessivo"); } catch(NumberFormatException ex) { JOptionPane.showMessageDialog(null, "Input errato"); } catch(Eccezionale ex) // 4 { JOptionPane.showMessageDialog(null, "Attento: " + ex.getMessage()); } } public static double dividi(double x, double y) throws Eccezionale // 3 { if(y==0) throw new Eccezionale("Divisione per zero!"); // 2 else return x/y; } } class Eccezionale extends Exception // 1 { public Eccezionale(String s) { super(s); } }
Osserva
- extends, estende Exception
- throw, lancia
- throws, rilancia al metodo chiamante
- catch, cattura