-5

My function has thrown an IOException of the following format. How do I catch an exception written like this, I've used the standard try/catch blocks.

public static void loader(int[] arr, String file) throws IOException
{
    Scanner sc = new Scanner(file);
    for(int i = 0; sc.hasNextInt(16) ; ++i)
        arr[i] = sc.nextInt(16);
}
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66

5 Answers5

1

When you declare a checked Exception in the signature of a method, you don't need to handle it in this method. At the contrary, any method calling loader will have either to declare this Exception in its header or handling it in a try/catch block :

public void someMethod() throws IOException {
   loader(...);
}

// or

public void someMethod() {
   try {
     loader(...);
   } catch (IOException io) {
      //...
   }
}
Dici
  • 25,226
  • 7
  • 41
  • 82
0
try{
//method call
}
catch (IOException e) {
//handle Exception
}

Or by using try with resources if class that declares the method implements AutoClosable interface

Lucas
  • 3,181
  • 4
  • 26
  • 45
  • ok, what's wrong with my answer? I like when someone downvotes without leaving comment. – Lucas Sep 24 '14 at 21:52
0

Just fill your code with a try/catch method:

Like this:

public static void loader(int[] arr, String file) throws IOException
{

try{
    Scanner sc = new Scanner(file);
    for(int i = 0; sc.hasNextInt(16) ; ++i)
    arr[i] = sc.nextInt(16);
}catch(IOException e) {
//Do whatever you want with the exception
}

}

Cesar Villasana
  • 681
  • 3
  • 6
0

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.)

APerson
  • 8,140
  • 8
  • 35
  • 49
0

You have two choices:

  1. Handle the exception in the calling code:

    try {
        YourClass.loader(/*...relevant args...*/);
    }
    catch (IOException ioe) {
        // ...do something with `ioe`...
    }
    

    or,

  2. Handle the exception inside the method:

    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 (IO Exception ioe) {
            /*...do something with `ioe`....*/
        }
    }
    

Which you choose is up to you. It's frequently useful to handle exceptions at an outer level (#1), but it totally depends on the code and context.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875