import java.io.*; import java.util.*; public class ThrowException { public static double addFileContents(String path) throws NegativeValueException { Scanner sc=null; try { File file = new File (path); sc = new Scanner(file); double sum = 0.0; while(sc.hasNextDouble()) { double value = sc.nextDouble(); if(value<0) throw new NegativeValueException(); sum += sc.nextDouble(); } } catch(FileNotFoundException ex) { System.out.println("The specified file was not found at "+ path); } catch(NoSuchElementException ex) { System.out.println("The specified type of element was not found!"); } finally { System.out.println("Closing File!"); sc.close(); } return 0.0; } public static void main(String args[]) { try { double result = addFileContents(args[0]); System.out.println("Result is: "+result); } catch(IndexOutOfBoundsException ex) { System.out.println("No file has been specified from command line!\n"); } catch(NegativeValueException ex) { System.out.println("The file contains negative value"); } } }