java Chapter 6 Exceptions
java Chapter 6 Exceptions
1
Introduction
• Users have high expectations for the code we
produce.
• Users may use our programs in unexpected
ways.
• Due to design errors or coding errors, our
programs may fail in unexpected ways during
execution
2
Introduction
• It is our responsibility to produce quality code that does
not fail unexpectedly.
• Consequently, we must design error handling into our
programs.
• In many cases, handling an exception allows a program to
continue executing as if no problem had been
encountered.
• A more severe problem could prevent a program from
continuing normal execution, instead requiring it to notify
the user of the problem before terminating in a controlled
manner
3
Errors and Error Handling
• An Error is any unexpected result obtained from a
program during execution.
• Unhandled errors may manifest themselves as
incorrect results or behavior, or as abnormal
program termination.
• Errors should be handled by the programmer, to
prevent them from reaching the user.
4
Errors and Error Handling
• Some typical causes of errors:
– Memory errors (Example. memory incorrectly
allocated, memory leaks, “null pointer”)
– File system errors (e.g. disk is full, disk has been
removed)
– Network errors (e.g. network is down, URL does not
exist)
– Calculation errors (e.g. divide by 0)
– Array errors (i.e. accessing element –1)
– Conversion errors (e.g . convert ‘q’ to a number)
5
Attack of the Exception
public static int average(int[] a) {
int total = 0;
for(int i = 0; i < a.length; i++) {
total += a[i];
}
return total / a.length;
}
• Some examples:
– Performing illegal arithmetic
– Illegal arguments to methods
– Accessing an out-of-bounds array element
– Hardware failures
– Writing to a read-only file
7
Another Exception Example
Output:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 2
at ExceptionExample.main(ExceptionExample.java:4)
8
Exception Message Details
Example:
java.lang.ArrayIndexOutOfBoundsException: 2
at ExceptionExample.main(ExceptionExample.java:4)
try {
// code that might throw exception
}
catch ([Type of Exception] e) {
// what to do if exception is thrown
}
10
Exception Handling Example
13
Example
14
Program with Exception
handled
15
16
Exceptions Terminology
• ArrayIndexOutOfBoundsException
Processing array with index out of range
• NullPointerException
When object try to process object containing null value
• IllegalArgumentException
method argument is improper is some way
19
Checked Exceptions
21
Exception Class Hierarchy
All exceptions are instances of classes
that are subclasses of Exception
Exception
ArrayIndexOutOfBounds FileNotFoundException
NullPointerException MalformedURLException
IllegalArgumentException SocketException
etc. etc.
22
Unchecked Exceptions Checked Exceptions
Checked and Unchecked Exceptions
25
Finally Block cont…
• In programming languages such as C and C++, the
most common kind of resource leak is a memory leak.
• Java performs automatic garbage collection of memory
no longer used by programs, thus avoiding most
memory leaks.
• However, other types of resource leaks can occur.
• For example, files, database connections and network
connections that are not closed properly might not be
available for use in other programs
26
27
Keyword Summary