17 - Exceptions



  • Understand what exceptions are
  • Understand why exceptions are needed
  • Know how to handle exception
  • Recognize most common Java exceptions



Exercise 1 - Getting to know exceptions

  • In your main method, use a Scanner to get a user input, and try to convert it to Integer. What happens if you run your program and input something that's not convertible to an integer?

😎 Toggle Solution
Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine();

// if the user has inserted a string
// this will throw an exception
  • In your main method, create a List<String> and print the string at position 3 in the list. What happens when you run your program?

😎 Toggle Solution
List<String> emptyList = new ArrayList<>();

// this will throw an exception in any case!
  • Try reading a file using a FileInputStream. Does it work? If not, why not? Advice: You can try reading a file from /some/path like this:
// this does not work just like this, since FileNotFoundException is a checked exception
FileInputStream inputStream = new FileInputStream(new File("/some/path"));
// do stuff with inputStream

Exercise 2 - Handling exceptions

  • How can you check that the user has actually entered an integer? How can you use exception-catching to solve this issue?

😎 Toggle Solution
Scanner scanner = new Scanner(System.in);
try {
	String userInput = scanner.nextLine();
} catch (NumberFormatException nfe) {
	// oh oh! something went wront
	// let's just print the stack trace
  • Can you make your Pizza/Moma/Bank code from last week more reliable by catching exceptions on user input?

Exercise 3 - finally clause

  • Can you close the inputStream from exercise 1 after using it? You can print "Closing input stream" when calling inputStream.close() to make sure you're actually doing it!

😎 Toggle Solution
FileInputStream inputStream = null;
try {
    System.out.println("Opening stream from file");
    inputStream = new FileInputStream(
            new File("/some/path"));

    // what if an exception happens here?

} catch (FileNotFoundException fnfE) {
    System.out.println("Something bad happened: " + fnfE.getMessage());
} finally {
    System.out.println("Closing input stream");

Java also offers a special syntax for classes that implement the AutoCloseable (or Closeable) interfaces. We're lucky, because FileInputStream does! You can find more info on how this works on the official Java tutorials.

try (FileInputStream inputStream = new FileInputStream(new File("/some/path"))) {

    // anything bad happening here doesn't matter
    // the input stream will be closed at the end of the block
    // without us having to do anything!
} catch (IOException e) {
    // we still need to catch exceptions thrown by the inputstream


Let's make a silly calculator. A calculator that can only do very few, very simple operations.

When the application starts, the user should be asked which operation they want to perform. The two following operations are possible:

  1. Divide 3 by a number entered by the user (3/x).
  2. Calculate the square root of a number provided by the user (√x)
  3. Sum 5 to a number entered by the user(5+x).

After the user has chosen one of these operations, they should be asked to enter the number they want to use for their operation (x). After entering the number, the result should be shown!

  • Remember: It's not possible to divide by 0, and the square root is only defined for positive numbers.
  • Try to keep your code clean! Use different methods for the various operations if possible.

Additional Resources

Last Updated: 10/27/2019, 6:38:47 PM