CMP2004 Advanced Programming: Lecture 6 Exception Handling
CMP2004 Advanced Programming: Lecture 6 Exception Handling
Advanced Programming
LECTURE 6 EXCEPTION HANDLING
Errors in Programs
▪Golden rules of Programming
▪ Errors occur in all software programs
▪How to deal with errors
▪ Try to minimize their occurrence (bug checking)
▪ Data validation (prevent invalid data)
▪ Handle them as they occur
▪Errors can be dealt with at place error occurs
▪ Easy to see if proper error checking implemented
▪ Harder to read application itself and see how code works
Error handling (old way)
if (file exists) {
open file;
while (there is more records to be processed) {
if (no IO errors) {
process the file record
}
else {
handle the errors
}
}
if (file is opened)
close the file;
}
else {
report the file does not exist;
}
Sample Code
public static void main(String[] args) {
// TODO Auto-generated method stub