Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions Complicate Code

Similar presentations


Presentation on theme: "Exceptions Complicate Code"— Presentation transcript:

1 Exceptions Complicate Code
protected void closeStreams() { if (currentObjectStream == null) { return; } try { currentObjectStream.close(); catch (IOException e) { Log.printf("couldn't close ObjectOutputStream for '%s'", currentFileName); currentFileStream.close(); Log.printf("couldn't close FileOutputStream for '%s'", currentFileName = null; currentFileStream = null; currentObjectStream = null; CS 190 Lecture Notes: Exception Handling

2 Exceptions Complicate Code
try ( FileInputStream fileStream = new FileInputStream(fileName); BufferedInputStream bufferedStream = new BufferedInputStream(fileStream); ObjectInputStream objectStream = new ObjectInputStream(bufferedStream); ) { for (int i = 0; i < tweetsPerFile; i++) { tf.tweets.add((Tweet) objectStream.readObject()); } catch (FileNotFoundException e) { ... catch (ClassNotFoundException e) { catch (EOFException e) { // Not a problem: not all tweet files have full set of tweets. catch (IOException e) { catch (ClassCastException e) { “Try with resources” CS 190 Lecture Notes: Exception Handling


Download ppt "Exceptions Complicate Code"

Similar presentations


Ads by Google