Spaces:
Running
Running
TurkuBasicOOPinJava
/
Week 3: Objects, files and exceptions
/18a 3 Types Exceptions, TRY-CATCH, throws FileNotFoundException
| try-catch block | |
| try block is used to perform an operation that may produce a run-time error, i.e. an exception. | |
| The exception is caught in the catch block following the try block. | |
| In addition, when the operation that might cause the error is written in parentheses after the try statement (as in the previous examples), | |
| Java AUTOMATICALLY takes care of CLOSing the RESOURCE. | |
| This prevents the file from being accidentally left open. | |
| In addition to reading files, the mechanism is useful in other ways. | |
| For example, if you try to convert an input that does not contain just numbers into an integer, the execution will give an error: | |
| import java.util.Scanner; | |
| public class Example { | |
| public static void main(String[] args){ | |
| Scanner reader = new Scanner(System.in); | |
| System.out.print("Give a number: "); | |
| int num = Integer.valueOf(reader.nextLine()); // this expects an Integer | |
| System.out.println("The number doubled is " + num * 2); | |
| } | |
| } | |
| Example output: | |
| Give a number: eight | |
| Exception in thread "main" java.lang.NumberFormatException: For input string: "eight" | |
| at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) | |
| at java.base/java.lang.Integer.parseInt(Integer.java:658) | |
| at java.base/java.lang.Integer.valueOf(Integer.java:989) | |
| at Example.main(Example.java:10) | |
| VS | |
| preparing for an error situation - TRY CATCH | |
| import java.util.Scanner; | |
| public class Example { | |
| public static void main(String[] args){ | |
| Scanner reader = new Scanner(System.in); | |
| try { | |
| System.out.print("Give a number: "); | |
| int num = Integer.valueOf(reader.nextLine()); | |
| System.out.println("The number doubled is " + num * 2); | |
| } | |
| catch (NumberFormatException e) { | |
| System.out.println("You didn't give a number!"); | |
| } | |
| } | |
| } | |
| Example output: | |
| Give a number: eight | |
| You didn't give a number! | |
| ================================================================== | |
| 3 types of errors in Java: | |
| 1 | |
| TRANSLATION errors - these must be fixed before the program can run. | |
| For example, a MISSPELLED variable name or a MISSING RETURN statement. | |
| 2 | |
| RUNTIME errors that MUST be HANDLED - for example, a FileNotFoundException that may be thrown by a FILE operation | |
| 3 | |
| RUNTIME errors that DO NOT NEED to be HANDLED - for example, NullPointerException. | |
| These errors are usually not caught, but are allowed to abort the execution of the program. | |
| Errors are also more typically caused by programmer error. | |
| Errors in category '2' must be handled by a try-catch block or passed up to the caller. | |
| For example, a method that reads a file may pass the error to the caller instead of handling the error itself (in the program). | |
| The forwarding is done in the method signature with the 'throws' keyword. | |
| import java.util.Scanner; | |
| public class Example { | |
| public static void main(String[] args){ | |
| try { | |
| printFile("names.txt"); | |
| } | |
| catch (FileNotFoundException e) { | |
| System.out.println("Error: " + e); | |
| } | |
| } | |
| // HERE - TOPUP method with 'throws' | |
| public static void printFile(String name) throws FileNotFoundException{ | |
| File file = new File(name); //name = filename and extension | |
| Scanner reader = new Scanner(file); | |
| while (reader.hasNextLine()) { | |
| System.out.println(reader.nextLine()); | |
| } | |
| // now reader must be closed by the programmer | |
| reader.close(); | |
| } | |
| } | |