Big Java Late Objects
Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 7, Problem 14RE

Explanation of Solution

Explanation:

  • The “try”-with-resources statement ensures that a resource is closed when the statement ends normally or abruptly.
  • To use this statement, the programmer needs to declare the needed resources within the parenthesis, and the built resource will be closed automatically at the end of the block.
  • When closing resources, the programmer must use “try”-with-resources instructions.
  • Consider the following sequence of statements:

//Create an instance variable

PrintWriter pout = new PrintWriter(file_name);

//Call the method

writeData(pout);

//Call close() method

pout...

Explanation of Solution

Type of exception being caught:

  • An exception is a problem that comes during the program execution.
  • An exception caught by a surrounding “catch” clause depends on the exception type being caught by the “catch” clause...

Explanation of Solution

A sample program:

  • When the programmer uses a resource, which should be closed, like a “PrintWriter”, the programmer requires to be careful in the presence of exceptions.
  • Consider the following sequence of statements:

//Create an instance variable

PrintWriter pout = new PrintWriter(file_name);

//Call the method

writeData(pout);

//Call close() method

pout.close();

  • Let’s assume that one of the methods prior to the last line throws an exception.
  • Then the invocation to the method “close()” is never executed.
  • The data that was written to the stream may never end up in the file.
  • Therefore, the programmer uses the try-with-resources statement...

Blurred answer
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education