Your method, as it is, just punts the IOException
to the next method on the call stack. In that case, your calling code would need to handle the exception:
public void foo() {
// assuming your method is in the Bar class
int[] secretCodes = {};
try {
Bar.loader(secretCodes, new File("C:\\tmp.txt");
} catch(IOException ex) {
ex.printStackTrace();
}
}
To use a try/catch block, though, you should remove the IOException
from the method signature. Then, you would no longer need to have the code that calls your method include a try/catch block.
In this case, your code would look like this:
public static void loader(int[] arr, String file)
{
try {
Scanner sc = new Scanner(file);
for(int i = 0; sc.hasNextInt(16) ; ++i)
arr[i] = sc.nextInt(16);
} catch(IOException ex) {
ex.printStackTrace(); // Or however you want to handle the exception
}
}
Note that you no longer need a throws IOException
in the method signature, since it's already handled in the method. (Unless, of course, your method throws one outside the try block.)