To recover from an exceptional situation you use a try-construct. A basic form is
try { code whose exceptions are caught } catching(Exception e) { what to do if an exception occurs }
For example, below is a main method that gets a file name from the command line. It opens that file and reads one integer n, then closes the file and runs method ProcessData(n) (not shown here).
But things can go wrong. The command line might not contains a file name. The file might not exist. It might exist, but not be readable. It might be a folder. Even if it is possible to read the file, there might not be any characters in it. Even if there are characters, they might not look like an integer.
All but one of those possibilities are handled by one handler here, which just says that the program was not able to do its job for some unknown reason. The one handled separately is the possibility that there is no file name in the command line.
public static void main(String[] args) { if(args.length() == 0) { System.out.println("Provide a file name on the command line."); } else { try { Scanner s = new Scanner(new File(args[0])); int n = s.nextInt(); ProcessData(n); } catch(Exception e) { System.out.println("Cannot carry out job."); } } }
You typically want to look more closely at just what went wrong. When an exceptional situation arises, there is a value, called an exception, that indicates at least partially what went wrong. We say that a method throws the exception. Phrase catch(Exception e) means to catch any exception that is normally catchable, and to call the exception e. But you can ask to catch only particular exceptions. Here are some relevant exceptions.
Scanner method nextInt() throws exception InputMismatchException if there is information available, but it does not look like an integer.
Scanner method nextInt() throws exception NoSuchElementException if there is no information available at all.
The Scanner constructor throws exception FileNotFoundException if the file does not exist.
The following has a catch part for each of the above exceptions.
public static void main(String[] args) { if(args.length() == 0) { System.out.println("Provide a file name on the command line."); } else { try { Scanner s = new Scanner(new File(args[0])); int n = s.nextInt(); ProcessData(n); } catch(FileNotFoundException e) { System.out.printf("I cannot open file %s for reading.", args[0]); } catch(NoSuchElementException e) { System.out.printf("File %s is empty.", args[0]); } catch(InputMismatchException e) { System.out.printf("File %s does not contain an integer", args[0]); } } }
Java has two kinds of exceptions. Exception matches a kind of exception that usually indicates something wrong with the environment in which the program runs; those are normally caught by a program. Error matches an exception that usually indicates there is something wrong with the program; those are normally not caught by the program, though they can be caught. To catch any error exception, use catch(Error e). To catch any exception at all, use catch(Throwable e).