<<< The syntax of the try statement | Index | Try with resources >>> |
A method that catches two types of exceptions and uses a finally clause
public static String readFirstLine( String path ) { RandomAccessFile in = null; try { in = new RandomAccessFile( path, "r" ); // may throw FileNotFound String line = in.readLine(); // may throw IOException return line; } catch ( FileNotFoundException ex ) { System.out.println( "File not found" ); return null; } catch ( IOException ex ) { System.out.println( "I/O error occurred" ); return null; } finally { try { if ( in != null ) in.close(); // may throw IOException } catch ( Exception ex ) { System.out.println( "Unable to close file." ); } } }
<<< The syntax of the try statement | Index | Try with resources >>> |