Professional Documents
Culture Documents
See en.wikipedia.org/wiki/Murphy’s_law
1
Java Exception Handling
• Apps occasionally encounter problems that just can’t be ignored, e.g.
• Attempting to open a file
that’s not available
Error
File Not
Found
2
Java Exception Handling
• Apps occasionally encounter problems that just can’t be ignored, e.g.
• Attempting to open a file
that’s not available
• Trying to call a method or access Error
a field via a null reference
NullPointer
Exception
3
Java Exception Handling
• Apps occasionally encounter problems that just can’t be ignored, e.g.
• Attempting to open a file
that’s not available
• Trying to call a method or access Error
a field via a null reference
• Indexing before or after the ArrayIndex
valid range of an array OutOfBounds
4
Java Exception Handling
• Exception handling separates
control flow paths of “normal”
processing versus “anomalous”
processing
See en.wikipedia.org/wiki/Exception_handling
5
Java Exception Handling
• Java exception handling is supported
by an extensible hierarchy of classes &
a set of virtual machine mechanisms
See eskatos.wordpress.com/2009/09/30/how-is-exception-handling-implemented-in-jvms
6
Java Exception Handling
• If an error condition occurs, an “exception” can be thrown to notify that a
problem occurred
7
Java Exception Handling
• If an error condition occurs, an “exception” can be thrown to notify that a
problem occurred
• Appropriate actions when catching
an exception include interacting with
the user, logging, or exiting the app
8
Java Exception Handling
• Any code can report an public class Vector<E> {
exception via “throw” ...
public Vector(int initialCapacity,
......) {
if (initialCapacity < 0)
throw new
IllegalArgumentException
("Illegal Capacity: "
+ initialCapacity);
...
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
9
Java Exception Handling
• Any code can report an public class Vector<E> {
exception via “throw” ...
public Vector(int initialCapacity,
......) {
if (initialCapacity < 0)
throw new
IllegalArgumentException
("Illegal Capacity: "
+ initialCapacity);
...
}
...
10
Java Exception Handling
• Any code can report an public class Vector<E> {
exception via “throw” ...
public Vector(int initialCapacity,
......) {
if (initialCapacity < 0)
throw new
IllegalArgumentException
("Illegal Capacity: "
+ initialCapacity);
...
}
...
11
Java Exception Handling
• Any code can report an public class Vector<E> {
exception via “throw” ...
public int lastIndexOf(Object o,
int index){
if (index >= mCount)
throw new
IndexOutOfBoundsException
(index + " >= "+ mCount);
...
}
12
Java Exception Handling
• Any code can report an public class Vector<E> {
exception via “throw” ...
public int lastIndexOf(Object o,
int index){
if (index >= mCount)
throw new
IndexOutOfBoundsException
(index + " >= "+ mCount);
...
}
13
Java Exception Handling
• The virtual machine performs
a number of steps when an
exception is thrown
14
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
15
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
16
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
17
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
18
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
Throws calls
IOException
method4() throw
IOException {…}
19
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
1. Normal program execution
stops method2() throws
Call Stack
IOException {…}
calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
20
Java Exception Handling
• The virtual machine performs
a number of steps when an method1()
exception is thrown
calls
1. Normal program execution
stops method2() throws
Call Stack
IOException {…}
2. Control then transfers to
appropriate handler calls
method3() throws
IOException {…}
calls
The virtual machine searches
up the runtime call stack to method4() throw
find exception handler IOException {…}
21
Java Exception Handling
• The virtual machine performs Catches
a number of steps when an method1()
IOException
exception is thrown
calls
1. Normal program execution
stops method2() throws
Call Stack
IOException {…}
2. Control then transfers to
appropriate handler calls
method3() throws
IOException {…}
calls
method4() throw
IOException {…}
22
Java Exception Handling
• A “try block” encloses regions of public class MyClass {
code that may throw exceptions public MyClass() {
try {
Vector<String> v =
new Vector<>(-1);
} catch
(IllegalArgumentException e){
...
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/try.html
23
Java Exception Handling
• A “try block” encloses regions of public class MyClass {
code that may throw exceptions public MyClass() {
try {
• A try block contains one or Vector<String> v =
more statements that may new Vector<>(-1);
throw an exception } catch
(IllegalArgumentException e){
...
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/try.html
24
Java Exception Handling
• A “try block” encloses regions of public class MyClass {
code that may throw exceptions public MyClass() {
try {
• A try block contains one or Vector<String> v =
more statements that may new Vector<>(-1);
throw an exception } catch
(IllegalArgumentException e){
...
}
...
25
Java Exception Handling
• A “try block” encloses regions of public class MyClass {
code that may throw exceptions public MyClass() {
try {
• A try block contains one or Vector<String> v =
more statements that may new Vector<>(-1);
throw an exception } catch
(IllegalArgumentException e){
...
}
...
26
Java Exception Handling
• One or more “catch blocks” can public class MyClass {
be associated with a try block public MyClass() {
try {
Vector<String> v =
new Vector<>(-1);
} catch
(IllegalArgumentException e){
...
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
27
Java Exception Handling
• One or more “catch blocks” can public class MyClass {
be associated with a try block public MyClass() {
try {
• A catch block defines an Vector<String> v =
exception handler that handles new Vector<>(-1);
the type of exception indicated } catch
by its argument (IllegalArgumentException e){
...
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
28
Java Exception Handling
• One or more “catch blocks” can public class MyClass {
be associated with a try block public MyClass() {
try {
• A catch block defines an Vector<String> v =
exception handler that handles new Vector<>(-1);
the type of exception indicated } catch
by its argument (IllegalArgumentException e){
...
}
...
29
Java Exception Handling
• One or more “catch blocks” can public class MyClass {
be associated with a try block public MyClass() {
try {
• A catch block defines an Vector<String> v =
exception handler that handles new Vector<>(-1);
the type of exception indicated } catch
by its argument (IllegalArgumentException e){
...
}
...
30
Java Exception Handling
• A “finally block” always runs public class SomeClass {
when the try block exits ...
private final ReentrantLock
lock = new ReentrantLock();
...
public void someMethod() ... {
final ReentrantLock lock
= this.lock;
lock.lock();
try {
// ...
} finally {
lock.unlock();
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
31
Java Exception Handling
• A “finally block” always runs public class SomeClass {
when the try block exits ...
private final ReentrantLock
lock = new ReentrantLock();
...
public void someMethod() ... {
final ReentrantLock lock
Runs regardless of whether = this.lock;
an exception is thrown or lock.lock();
if the try block returns try {
// ...
} finally {
lock.unlock();
}
...
32
Java Exception Handling
• A “try-with-resources” try block public class SomeClass {
declares one or more resources ...
that are closed after block exits String readFirstLineFromFile
(String path)
throws IOException {
try (BufferedReader bufReader =
new BufferedReader
(new FileReader(path)))
{ return bufReader.readLine(); }
}
...
See docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
33
Java Exception Handling
• A “try-with-resources” try block public class SomeClass {
declares one or more resources ...
that are closed after block exits String readFirstLineFromFile
(String path)
throws IOException {
try (BufferedReader bufReader =
new BufferedReader
(new FileReader(path)))
{ return bufReader.readLine(); }
}
...
34
Java Exception Handling
• A “try-with-resources” try block public class SomeClass {
declares one or more resources ...
that are closed after block exits String readFirstLineFromFile
(String path)
throws IOException {
try (BufferedReader bufReader =
new BufferedReader
(new FileReader(path)))
{ return bufReader.readLine(); }
}
...
35
Java Exception Handling
• A “try-with-resources” try block public class SomeClass {
declares one or more resources ...
that are closed after block exits String readFirstLineFromFile
(String path)
throws IOException {
try (BufferedReader bufReader =
new BufferedReader
(new FileReader(path)))
{ return bufReader.readLine(); }
}
...
bufReader is automatically
closed when the block exits
36
Java Exception Handling
• Java exceptions are organized
as a class hierarchy
37
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void aMethod() {
try { ... }
• Handlers can be specified for catch
different exception subclasses (IndexOutOfBoundsException i)
{ ... }
catch
(IllegalArgumentException e)
{ ... }
}
38
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void aMethod() {
try { ... }
• Handlers can be specified for catch
different exception subclasses (IndexOutOfBoundsException i)
{ ... }
catch
(IllegalArgumentException e)
{ ... }
}
39
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void aMethod() {
try { ... }
• Handlers can be specified for catch
different exception subclasses (IndexOutOfBoundsException i)
{ ... }
catch
(IllegalArgumentException e)
{ ... }
}
40
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
41
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
42
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
43
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
44
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
45
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
46
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
47
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
48
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void method1() {
try { method2(); }
• Handlers can be specified for catch (SomeException e) {
different exception subclasses // do something w/exception
• If a handler isn’t specified for e.printStackTrace();
the exception thrown, the next }
matching handler up the
public void method2()
runtime stack get called
{ method3(); }
49
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void methodA() {
...
• Handlers can be specified for }
different exception subclasses
• If a handler isn’t specified for public void methodB() {
the exception thrown, the next try { methodA(); }
matching handler up the catch (Exception e) {
// do something w/exception
runtime stack get called
}
• To handle every possible type }
of exception, catch Exception
See developer.android.com/reference/java/lang/Exception.html
50
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void methodA() {
...
• Handlers can be specified for }
different exception subclasses
• If a handler isn’t specified for public void methodB() {
the exception thrown, the next try { methodA(); }
matching handler up the catch (Throwable e) {
// Every recoverable runtime
runtime stack get called
// error and exception will
• To handle every possible type // be caught
of exception, catch Exception e.printStackTrace();
• To hancle every possible type of }
}
error, catch Throwable
See developer.android.com/reference/java/lang/Throwable.html
51
Java Exception Handling
• Java exceptions are organized public class MyClass {
as a class hierarchy public void methodA() {
...
• Handlers can be specified for }
different exception subclasses
• If a handler isn’t specified for public void methodB() {
the exception thrown, the next try { methodA(); }
matching handler up the catch (Throwable t) {
// Every recoverable runtime
runtime stack get called
// error and exception will
• To handle every possible type // be caught
of exception, catch Exception t.printStackTrace();
• To handle every possible type }
}
of error, catch Throwable
See developer.android.com/reference/java/lang/Throwable.html#printStackTrace()
52
Java Exception Handling
• There are two types of exceptions:
53
Java Exception Handling
• There are two types of exceptions: public class Vector<E> {
private void writeObject
• Checked exceptions (java.io.ObjectOutputStream s)
• Methods declaring “checked throws java.io.IOException {
exceptions” require callers to ...
provide an exception handler }
...
See en.wikibooks.org/wiki/Java_Programming/Checked_Exceptions
54
Java Exception Handling
• There are two types of exceptions: public class Vector<E> {
private void writeObject
• Checked exceptions (java.io.ObjectOutputStream s)
• Methods declaring “checked throws java.io.IOException {
exceptions” require callers to ...
provide an exception handler }
...
55
Java Exception Handling
• There are two types of exceptions: public class Vector<E> {
private void writeObject
• Checked exceptions (java.io.ObjectOutputStream s)
• Methods declaring “checked throws java.io.IOException {
exceptions” require callers to ...
provide an exception handler }
...
void method1(Vector<Integer> v
ObjectOutputStream s) {
try { v.writeObject(s); }
catch (IOException ioe) {
...
}
}
...
56
Java Exception Handling
• There are two types of exceptions: public class Vector<E> {
private void writeObject
• Checked exceptions (java.io.ObjectOutputStream s)
• Methods declaring “checked throws java.io.IOException {
exceptions” require callers to ...
provide an exception handler }
...
void method1(Vector<Integer> v
ObjectOutputStream s) {
try { v.writeObject(s); }
catch (IOException ioe) {
...
}
}
...
57
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
See docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
58
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
59
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
60
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
61
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
62
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
63
Java Exception Handling
• There are two types of exceptions: public class MyClass {
public void methodA(int index) {
• Checked exceptions if (index < 0)
• Unchecked exceptions throw new
IndexOutOfBoundsException();
• Can be thrown without a ...
method declaring it }
64
Java Exception Handling
See docs.oracle.com/javase/tutorial/essential/exceptions
65