Skip to content

Instantly share code, notes, and snippets.

@jhare
Created March 11, 2016 03:37
Show Gist options
  • Select an option

  • Save jhare/ae912b3bbcabb491b76a to your computer and use it in GitHub Desktop.

Select an option

Save jhare/ae912b3bbcabb491b76a to your computer and use it in GitHub Desktop.
exception-example.java
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("Caught IndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment