CS1101: Programming Methodology Aaron Tan

advertisement
CS1101: Programming Methodology
http://www.comp.nus.edu.sg/~cs1101x/
Aaron Tan
This is Week 12
 Week 10:
 Chapter 11: Type Details and Alternate
Coding Mechanisms
 This week:
 Chapter 14: Exception Handling
 Chatper 15: Files
2
Writing robust programs (1/3)

Suppose you have this statement
int value = stdIn.nextInt();

So far, we are assuming that the user always
follows instructions and never enters the wrong
data. But what if the user enters the following
in response to the above statement?
A
string (eg: “apple”)?
A
real number (eg: 12.3)?
3
Writing robust programs (2/3)

Refer to WrongInput.java
 User
enters a string:
An exception is thrown.
Enter an integer: apple
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at WrongInput.main(WrongInput.java:9)
 User
enters real number:
Enter an integer: 12.34
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at WrongInput.main(WrongInput.java:9)
4
Writing robust programs (3/3)

An exception is thrown when an error occurs. If
the exception is not caught, the program
crashes.

We would like to catch the exception so that
the program does not crash.

Let’s go on to Chapter 14 to see how to handle
exceptions.
5
Using throws to postpone catch


Refer to StudentList2.java and
StudentList2Driver.java
For checked exceptions, if you do not have a
try-catch block, then you must add ‘throws’ at
the heading of the method, or it will not
compile.
6
The finally Block

Sometimes there are situations where we need
to take certain actions regardless of whether
an exception is thrown or not. For example, we
need some “cleanup” code, like closing a file.
Use a finally block.

Refer to WriteToFile.java

7
Summary: try-catch block
Exception
try {
<t-stmt-1>
<t-stmt-2>
No Exception
try {
<t-stmt-1>
<t-stmt-2>
Assume <t-stmt-3>
throws an exception.
<t-stmt-3>
<t-stmt-4>
. . .
<t-stmt-n>
<t-stmt-3>
} catch (Exception e) {
<c-stmt-1>
. . .
<c-stmt-m>
}
<next stmt>
Ack: Thomas Wu
<t-stmt-4>
Remaining
statements in the
try block is skipped.
. . .
<t-stmt-n>
} catch (Exception e) {
<c-stmt-1>
Statements in the
catch block are
executed.
And the execution
continues to the
next statement
All statements in
the try block are
executed.
. . .
<c-stmt-m>
Statements in the
catch block are
skipped.
}
<next stmt>
8
Summary: multiple catch blocks
Exception
try {
<t-stmt-1>
<t-stmt-2>
No Exception
Assume <t-stmt-3>
throws an exception
and <catch-block-3>
is the matching block.
try {
<t-stmt-1>
<t-stmt-2>
<t-stmt-3>
<t-stmt-4>
. . .
<t-stmt-n>
<t-stmt-3>
<t-stmt-4>
Remaining
statements in the
try block is skipped.
}
All statements in
the try block are
executed and throw
no exceptions.
. . .
<t-stmt-n>
}
<catch-block-1>
<catch-block-2>
<catch-block-3>
. . .
<catch-block-m>
}
<catch-block-1>
<catch-block-2>
Statements
in the
matching
catch block
are executed.
<catch-block-3>
All catch
blocks are
skipped.
. . .
<catch-block-m>
}
<next stmt>
Ack: Thomas Wu
<next stmt>
9
Summary: with finally block
Exception
No Exception
Assume <t-stmt-i>
try {
throws an exception
<t-stmt-1>
and <catch-block-i> is
. . .
the matching block.
<t-stmt-i>
. . .
<t-stmt-n>
}
<catch-block-1>
. . .
<catch-block-i>
. . .
<catch-block-m>
try {
<t-stmt-1>
. . .
<t-stmt-i>
. . .
<t-stmt-n>
}
<catch-block-1>
. . .
<catch-block-i>
. . .
<catch-block-m>
} finally {
. . .
}
<next stmt>
} finally {
. . .
}
<next stmt>
Ack: Thomas Wu
finally block is
executed.
finally block is
executed.
10
Summary: Hierarchy of exceptions

There are over 60 classes in the hierarchy. Here are just some.
Ack: Thomas Wu
11
Handling files in UNIX
 We have seen file processing in Java.
 Let me introduce some features in UNIX
that handle files (we did it in our intro lab,
if you still remember!)
 This is not part of Java.
12
UNIX File Input Redirection

<:To redirect input from a file
$ java MyProgram < inputFile
The text file inputFile
contains the input data.
$ java MySum
Enter 3 integers: 4 2 3
Sum = 9
$ cat sum.in
4 2 3
$ java MySum < sum.in
Enter 3 integers: Sum = 9
13
Example

MySum.java
import java.util.*;
class MySum {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter 3 integers: ");
int a = stdIn.nextInt();
int b = stdIn.nextInt();
int c = stdIn.nextInt();
int sum = a + b + c;
System.out.println("Sum = " + sum);
}
}
14
UNIX File Output Redirection

>:To redirect output to a file (>>: to append to a
file)
$ java MyProgram > outputFile
The text file outputFile
contains the output data.
$ java MySum
Enter 3 integers: 4 2 3
Sum = 9
$ java MySum > sum.out
4 2 3
$ cat sum.out
Enter 3 integers: Sum = 9
15
Announcement/Reminder
 Lab #5
 Deadline: 5 November (Wednesday), 2359
hr.
 PE
 Please refer to website
http://www.comp.nus.edu.sg/~cs1101x/3_ca/pe.html
 Consultation @ my office (COM1-03-12)
 Wednesday (5 Nov) 3 – 6 pm
16
This is Week 12
 Next week (last week!)
 Chapter 12: Aggregation, Composition, and
Inheritance
17
End of file
18
Download