Example - Opening a Datafile

Try/Catch Block for Opening a Datafile
 try
    {
        infile = new BufferedReader(new FileReader(inFileName));
    }
    catch   (IOException e)
    {
        System.err.println("Error - cannot open input file " + inFileName);
        return;
    }

Another common way to write this code is to handle different kinds of exceptions differently. Look at the additional code (in bold) below that shows how to do this.

Handling Different Exceptions for Opening a Datafile

 try
    {
        infile = new BufferedReader(new FileReader(inFileName));
    }
    catch    (FileNotFoundException e)
    {
        System.err.println("Error - input file " + inFileName + " not found.");
        return;
    }
    catch   (IOException e)
    {
        System.err.println("Error - cannot open input file " + inFileName);
        return;
    }