HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaModerate

IO and Scanner exception handling

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andhandlingscannerexception

Problem

I have created a sample file reading program as an exercise while learning how to implement good exception handling. I want to ask: is this a good approach or is there some other solution that I am not aware of?

import java.util.*;  // Needed for Scanner and InputMismatchException
import java.io.*;    // Needed for FileReader and FileNotFoundException

/**
 * This class reads two numbers from a file and 
 * print their sum.  
 */

public class ReadNum
{ 
   public static void main (String[] args) 
   {
       Scanner inFile = null;
       try
       {
           int num1, num2;  // To hold numbers read from the file
           int sum; //To hold sum

           // Create the scanner object for file input.
           inFile = new Scanner(new FileReader("sample.txt"));

           // Read numbers from the file.
           num1 = inFile.nextInt();
           num2 = inFile.nextInt();

           // Calculate total.
           sum = num1 + num2;

           // Print total.
           System.out.println("sum " + sum);
       } 
       catch (FileNotFoundException ex)
       {
           System.out.println("Error "+ ex);
       }
       catch (InputMismatchException ex)
       {
           System.out.println("Error "+ ex);
       }
       catch (Exception ex)
       {
           System.out.println("Error "+ ex);
       }
       finally
       {
           // Close the file.
           if(inFile != null)
                inFile.close();
       }
   }
}

Solution

Your code is interesting, in the sense that it appears to have been pulled from about 4 different text books all related to Java about 4 versions ago.... actually, make it Java 1.3.

It is consistent code, but everything is just.... old.

In some sense of order, the following strike me most:

  • multicatch



  • try with resources



  • don't catch Exception



  • don't just print the exception's toString, print the full stack.



  • Brace on same line



  • don't *-import.



  • don't pre-declare variables... declare at use-time.



  • redundant comments



On the other hand, your variable names are great, and the basic concept is sound.

Here's my version of your code:

import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ReadNum {
    public static void main(String[] args) {
        try (Scanner inFile = new Scanner(new FileReader("sample.txt"))) {

            int num1 = inFile.nextInt();
            int num2 = inFile.nextInt();

            int sum = num1 + num2;

            System.out.println("sum " + sum);
        } catch (IOException | InputMismatchException ex) {
            ex.printStackTrace();
        }
    }
}

Code Snippets

import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ReadNum {
    public static void main(String[] args) {
        try (Scanner inFile = new Scanner(new FileReader("sample.txt"))) {

            int num1 = inFile.nextInt();
            int num2 = inFile.nextInt();

            int sum = num1 + num2;

            System.out.println("sum " + sum);
        } catch (IOException | InputMismatchException ex) {
            ex.printStackTrace();
        }
    }
}

Context

StackExchange Code Review Q#90287, answer score: 10

Revisions (0)

No revisions yet.