KaiquanMah commited on
Commit
58510f2
·
verified ·
1 Parent(s): 45588d0

Create 18a 3 Types Exceptions, TRY-CATCH, throws FileNotFoundException

Browse files
Week 3: Objects, files and exceptions/18a 3 Types Exceptions, TRY-CATCH, throws FileNotFoundException ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ try-catch block
2
+ try block is used to perform an operation that may produce a run-time error, i.e. an exception.
3
+ The exception is caught in the catch block following the try block.
4
+
5
+ In addition, when the operation that might cause the error is written in parentheses after the try statement (as in the previous examples),
6
+ Java AUTOMATICALLY takes care of CLOSing the RESOURCE.
7
+ This prevents the file from being accidentally left open.
8
+
9
+
10
+
11
+ In addition to reading files, the mechanism is useful in other ways.
12
+ For example, if you try to convert an input that does not contain just numbers into an integer, the execution will give an error:
13
+
14
+ import java.util.Scanner;
15
+
16
+ public class Example {
17
+ public static void main(String[] args){
18
+ Scanner reader = new Scanner(System.in);
19
+ System.out.print("Give a number: ");
20
+ int num = Integer.valueOf(reader.nextLine()); // this expects an Integer
21
+
22
+ System.out.println("The number doubled is " + num * 2);
23
+ }
24
+ }
25
+
26
+ Example output:
27
+ Give a number: eight
28
+ Exception in thread "main" java.lang.NumberFormatException: For input string: "eight"
29
+ at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
30
+ at java.base/java.lang.Integer.parseInt(Integer.java:658)
31
+ at java.base/java.lang.Integer.valueOf(Integer.java:989)
32
+ at Example.main(Example.java:10)
33
+
34
+
35
+ VS
36
+
37
+
38
+
39
+ preparing for an error situation - TRY CATCH
40
+
41
+ import java.util.Scanner;
42
+
43
+ public class Example {
44
+ public static void main(String[] args){
45
+ Scanner reader = new Scanner(System.in);
46
+ try {
47
+ System.out.print("Give a number: ");
48
+ int num = Integer.valueOf(reader.nextLine());
49
+
50
+ System.out.println("The number doubled is " + num * 2);
51
+ }
52
+ catch (NumberFormatException e) {
53
+ System.out.println("You didn't give a number!");
54
+ }
55
+ }
56
+ }
57
+
58
+ Example output:
59
+ Give a number: eight
60
+ You didn't give a number!
61
+
62
+
63
+
64
+
65
+ ==================================================================
66
+
67
+
68
+
69
+ 3 types of errors in Java:
70
+
71
+ 1
72
+ TRANSLATION errors - these must be fixed before the program can run.
73
+ For example, a MISSPELLED variable name or a MISSING RETURN statement.
74
+
75
+ 2
76
+ RUNTIME errors that MUST be HANDLED - for example, a FileNotFoundException that may be thrown by a FILE operation
77
+
78
+ 3
79
+ RUNTIME errors that DO NOT NEED to be HANDLED - for example, NullPointerException.
80
+ These errors are usually not caught, but are allowed to abort the execution of the program.
81
+ Errors are also more typically caused by programmer error.
82
+
83
+
84
+
85
+
86
+
87
+ Errors in category '2' must be handled by a try-catch block or passed up to the caller.
88
+
89
+
90
+
91
+
92
+
93
+
94
+ For example, a method that reads a file may pass the error to the caller instead of handling the error itself (in the program).
95
+ The forwarding is done in the method signature with the 'throws' keyword.
96
+
97
+ import java.util.Scanner;
98
+
99
+ public class Example {
100
+ public static void main(String[] args){
101
+ try {
102
+ printFile("names.txt");
103
+ }
104
+ catch (FileNotFoundException e) {
105
+ System.out.println("Error: " + e);
106
+ }
107
+ }
108
+
109
+ // HERE - TOPUP method with 'throws'
110
+ public static void printFile(String name) throws FileNotFoundException{
111
+ File file = new File(name); //name = filename and extension
112
+ Scanner reader = new Scanner(file);
113
+
114
+ while (reader.hasNextLine()) {
115
+ System.out.println(reader.nextLine());
116
+ }
117
+
118
+ // now reader must be closed by the programmer
119
+ reader.close();
120
+ }
121
+ }
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+