Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Introductory Programming In Java
Comp6700/Comp2140
Week 6, 2016
Henry Gardner/ Alexei Khorev/ Charles Martin
Next week we will be running the mid-semester lab exam. Alexei with 
give you an overview of the way this lab exam will work and take you 
through a sample exam today.
Next week is a short week and Monday’s lab class will sit their lab exam 
on Friday 1 April between 1-3 pm. The other lab classes will sit this 
exam at their regular times. Students are not permitted to discuss the 
lab exam until after the Friday lab session. There will be no lecture on 
Friday of next week.
Homework 4 can be submitted through the Wattle site by 5pm on Friday 
1 April. 
The week after next is the mid-semester break. The assignment is due 
during the mid-semester break.
How are you coping……?
2
Java Exception Handling
This week’s Henry lecture
3
Some runtime errors are bugs. They should not happen!
Runtime errors
4
What if the file exists but cannot be opened?
What if the file named is actually a directory?
What does the API say…?
Some errors are waiting to happen
5
Lots of potential error situations happen; particularly 
when Java interacts with the world.
The Java API forces the programmer to “check” these 
situations and to do something when they occur
Consequences of Java exception handling 
for the Programmer
6
Special “exception objects” are constructed when code 
inside methods causes a runtime error to occur.
These exception objects are thrown “up” through the 
call stack, possibly to the JVM itself, in order to do 
something about the error (including crashing the code!)
Method signatures can explicitly throw exception 
objects
If these exception objects are “checked exceptions”, 
then the compiler forces any code that calls such 
methods to do something about these exception 
objects
Catch and handle them….
…or throw them up further
Java “exceptions” are objects
7
8
9
try{…}catch{…}finally{…}
“try block”: programming for success
“catch block”: exception handling
Print out information from exception objects
Stop the program?
“finally block”: close files and tidy up
How to “handle” exceptions
10
ListOfNumbers.java
ListOfNumbers1.java
Comment out clauses and see what gets printed
Deliberately write past the end of file
Make OutFile.txt a directory
Throw rather than catch
Example codes – play with these
11
A newer version of the try block (Java SE 7)
Resources can be listed in the “try” statement. They will 
be automatically closed and tidied up regardless of 
whether an exception was thrown or not.
See ListOfNumbers2
Represents best practice:
Resources handled automatically; don’t need “finally” block
Don’t catch unchecked exception (index out of bounds)
Try with resources…
12
Use exceptions to respond gracefully to events in the 
real world. 
Print out desired information
Don’t crash your program unless you really need to
Use try-with-resources
Catch multiple exceptions from the most specific (first 
catch clause) to the most general (subsequent catch 
clauses) when moving up an exception hierarchy
Don’t catch unchecked exceptions (like we did!)
Fine to throw exceptions to another method
Don’t use exceptions for “routine” events (see next page)
Best practice - exceptions
13
EG: Don’t use exceptions to terminate reading of the 
end of input in the following context (left hand version 
is better)
14