17 - Exceptions
Michele
Goals
- Understand what exceptions are
- Understand why exceptions are needed
- Know how to handle exception
- Recognize most common Java exceptions
Slides
Exercises
Exercise 1 - Getting to know exceptions
- In your
main
method, use aScanner
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?
- In your
main
method, create aList<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/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
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
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 callinginputStream.close()
to make sure you're actually doing it!
Recap
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.