Download presentation
Presentation is loading. Please wait.
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.