File size: 3,580 Bytes
58510f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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();
    }
}