© Mikael Olsson 2022
M. OlssonJava 17 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-7371-5_21

21. Exception Handling

Mikael Olsson1  
(1)
Hammarland, Länsi-Suomi, Finland
 
Exception handling allows programmers to deal with unexpected situations that may occur in their programs. For example, the FileReader class in the java.io package is used to open a file. Creating an instance of this class will cause the IDE to give a reminder that the class’s constructor may throw a FileNotFoundException. Attempting to run the program will also cause the compiler to point this out.
import java.io.*;
public class MyClass
{
  public static void main(String[] args)
  {
    // Compile-time error
    FileReader file = new FileReader("missing.txt");
  }
}

Try-Catch

Get Java 17 Quick Syntax Reference: A Pocket Guide to the Java SE Language, APIs, and Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.