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
mainmethod, use a
Scannerto 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?
- In your
mainmethod, create a
List<String>and print the string at position 3 in the list. What happens when you run your program?
- Try reading a file using a
FileInputStream. Does it work? If not, why not? Advice: You can try reading a file from
// 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 inputStream.close();
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?
- Can you make your
Bankcode from last week more reliable by catching exceptions on user input?
Exercise 3 - finally clause
- Can you close the
inputStreamfrom exercise 1 after using it? You can print "Closing input stream" when calling
inputStream.close()to make sure you're actually doing it!
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:
- Divide 3 by a number entered by the user (3/x).
- Calculate the square root of a number provided by the user (√x)
- 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.