Java · Intermediate level

Intermediate level · Module 3 of 6 · 16 guided hours

Exceptions and File Input/Output in Java

Handle errors deliberately with exceptions, create your own exception types and read and write text and CSV files with java.nio.file.

Learning objectives

  • Distinguish checked and unchecked exceptions and handle each appropriately
  • Write custom exceptions and use try-with-resources
  • Read and write text and CSV files reliably

Topics

  • exception hierarchy
  • checked vs unchecked
  • try/catch/finally
  • custom exceptions
  • try-with-resources
  • java.nio.file.Files and Path
  • CSV parsing

Lessons

  1. Lesson 1 Handling Exceptions

    Understand the exception hierarchy, the difference between checked and unchecked exceptions, and how to catch and rethrow responsibly.

    Throwable, Exception, RuntimeExceptionchecked exceptionstack tracefinally

  2. Lesson 2 Custom Exceptions and try-with-resources

    Create exception types that describe your program's rules, and close files and other resources automatically.

    custom exception classexception messageAutoCloseabletry-with-resources

  3. Lesson 3 Reading and Writing CSV Files

    Use Path and Files to read and write text files, parse CSV lines into objects and report bad lines.

    Path and FilesreadAllLines / BufferedReaderCSV header and columnsUTF-8

Practical activity

Load a CSV file of student payments, skip and report malformed lines with their line numbers, and write a summary file of balances.

Expected outcomes

  • Programs that fail clearly instead of crashing silently
  • Reliable reading and writing of data files

Assessment

Practical: process a deliberately messy CSV file; marked on correct totals, reported errors and use of try-with-resources.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.