try { FileInputStream file = new FileInputStream("missing_file.txt"); // some code to read the file } catch (IOException e) { System.out.println("Error message: " + e.getMessage()); }
try { Socket socket = new Socket("localhost", 1234); // some code to communicate with the server } catch (IOException e) { System.out.println("Error message: " + e.getMessage()); }This code tries to establish a socket connection with a server. If an IOException occurs, the getMessage() method is used to print the error message to the console. Both of these examples use the IOException class, which is part of the java.io package library.