412 lines
5.8 KiB
Plaintext
412 lines
5.8 KiB
Plaintext
Application
|
||
Programming
|
||
Hend Alkittawi
|
||
|
||
Exception Handling
|
||
Introduction To Java Errors And
|
||
Exceptions
|
||
|
||
INTRODUCTION
|
||
-
|
||
|
||
In Java when things go wrong a java.lang.Exception object is
|
||
created.
|
||
|
||
-
|
||
|
||
For example,
|
||
-
|
||
|
||
if we add elements to an uninitialized arraylist
|
||
-
|
||
|
||
-
|
||
|
||
if we try to read from a file that doesn’t exist
|
||
-
|
||
|
||
-
|
||
|
||
FileNotFoundException
|
||
|
||
if we try to read past the end of the file
|
||
-
|
||
|
||
-
|
||
|
||
NullPointerException
|
||
|
||
IOException
|
||
|
||
if the file changes while we are reading it
|
||
-
|
||
|
||
IOException
|
||
|
||
THE CALL STACK
|
||
-
|
||
|
||
When a Java program runs, execution begins in the main()
|
||
method. The main() method creates objects and invokes methods
|
||
on them.
|
||
|
||
-
|
||
|
||
When execution moves to another method an entry is added to
|
||
the call stack.
|
||
|
||
-
|
||
|
||
When a method finishes executing, the entry
|
||
|
||
call stack
|
||
|
||
is removed from the call stack, and execution
|
||
returns to the next line in the main() method
|
||
-
|
||
|
||
obj.method()
|
||
|
||
this continues until the main method finishes
|
||
main()
|
||
|
||
THE CALL STACK
|
||
-
|
||
|
||
The call stack entry below, among other things, contains
|
||
-
|
||
|
||
the current method
|
||
|
||
-
|
||
|
||
where the call occurred in that method
|
||
|
||
NullPointerException:
|
||
at Student.getAverage(Student.java:79)
|
||
at Student.toString(Student.java:62)
|
||
at java.lang.String.valueOf(String.java:2615)
|
||
at java.io.PrintStream.print(PrintStream.java:616)
|
||
at java.io.PrintStream.println(PrintStream.java:753)
|
||
at Student.main(Student.java:120)
|
||
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|
||
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
|
||
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
|
||
at java.lang.reflect.Method.invoke(Method.java:585)
|
||
|
||
EXCEPTIONS IN JAVA
|
||
-
|
||
|
||
In Java, all exception classes inherit from the Exception class
|
||
|
||
-
|
||
|
||
Exceptions in Java are checked or unchecked!
|
||
|
||
-
|
||
|
||
Checked exceptions must be caught or thrown. Examples of
|
||
checked exceptions include:
|
||
|
||
-
|
||
|
||
-
|
||
|
||
IOException
|
||
|
||
-
|
||
|
||
FileNotFoundException
|
||
|
||
Unchecked exceptions should never be caught or thrown. Examples
|
||
of unchecked exceptions include:
|
||
-
|
||
|
||
NullPointerException
|
||
|
||
-
|
||
|
||
ArrayIndexOutOfBoundsException
|
||
|
||
EXCEPTIONS IN JAVA
|
||
|
||
EXCEPTION HANDLING
|
||
-
|
||
|
||
As developers, we must address any problems that might occur.
|
||
|
||
-
|
||
|
||
For unchecked exceptions, your code should follow best
|
||
practices in order to prevent exceptions occurrences. For
|
||
example
|
||
|
||
-
|
||
|
||
-
|
||
|
||
check for array bounds
|
||
|
||
-
|
||
|
||
check for null values
|
||
|
||
For checked exceptions, your code either throws the exception
|
||
or handles the exception with a try/catch block.
|
||
|
||
EXCEPTION HANDLING
|
||
-
|
||
|
||
Using try, catch, and finally blocks
|
||
-
|
||
|
||
Wrap all code that can cause a checked exception in try, catch
|
||
(and optionally finally) blocks
|
||
|
||
try {
|
||
|
||
try {
|
||
|
||
System.out.println("code here can cause
|
||
an exception");
|
||
} catch (Exception e) {
|
||
|
||
System.out.println("reading from a file …");
|
||
} catch (FileNotFoundException e) {
|
||
|
||
System.out.println("handle exception here");
|
||
System.out.println("handle exception here");
|
||
} finally { // optional
|
||
} finally { // optional
|
||
System.out.println("code that must be absolutely
|
||
executed after try block completes");
|
||
}
|
||
|
||
System.out.println("closing the file …");
|
||
}
|
||
|
||
EXCEPTION HANDLING
|
||
-
|
||
|
||
A try block can have multiple catch blocks.
|
||
|
||
-
|
||
|
||
The order of the catch blocks is important.
|
||
|
||
try {
|
||
|
||
try {
|
||
System.out.println("reading from a file …");
|
||
|
||
} catch (FileNotFoundException e) {
|
||
|
||
System.out.println("reading from a file …");
|
||
} catch (IOException e) {
|
||
|
||
System.out.println("code here will execute
|
||
when a FileNotFoundException is thrown!");
|
||
} catch (IOException e) {
|
||
|
||
System.out.println("code here will execute
|
||
when a FileNotFoundException is thrown!");
|
||
} catch (FileNotFoundException e) {
|
||
|
||
System.out.println("handle exception here");
|
||
|
||
System.out.println("code here will not execute
|
||
when a FileNotFoundException is thrown!");
|
||
|
||
} finally {
|
||
} finally {
|
||
System.out.println("closing the file …");
|
||
}
|
||
|
||
System.out.println("closing the file …");
|
||
}
|
||
|
||
THROWING EXCEPTIONS
|
||
-
|
||
|
||
An exception might be thrown, when there is nothing more you
|
||
can do about it!
|
||
public void methodA() {
|
||
try {
|
||
dangerZone();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
public void dangerZone() throws Exception {
|
||
throw new Exception();
|
||
}
|
||
}
|
||
|
||
EXCEPTION HANDLING EXAMPLE
|
||
import java.io.FileNotFoundException;
|
||
public class DemoExceptions {
|
||
public static void main(String[] args) {
|
||
try {
|
||
method(true);
|
||
System.out.println("returned from method()");
|
||
} catch (FileNotFoundException e) {
|
||
System.out.println("caught the exception, will handle it!");
|
||
e.printStackTrace();
|
||
} finally {
|
||
// code that must be absolutely executed after try block completes
|
||
System.out.println("finally will cleanup!");
|
||
}
|
||
}
|
||
public static void method(boolean exception) throws FileNotFoundException {
|
||
if(exception)
|
||
throw new FileNotFoundException();
|
||
System.out.println("method 1 executed successfully!");
|
||
}
|
||
}
|
||
|
||
HANDLING EXCEPTIONS
|
||
-
|
||
|
||
Handling exceptions improves the user experience!
|
||
|
||
-
|
||
|
||
Consider
|
||
|
||
-
|
||
|
||
-
|
||
|
||
Where can errors happen caused by our logic?
|
||
|
||
-
|
||
|
||
Where can exceptions happen?
|
||
|
||
-
|
||
|
||
Where can user error occur?
|
||
|
||
For each, how can we prevent or reduce these?
|
||
-
|
||
|
||
What would the user expect?
|
||
|
||
USER EXPERIENCE!
|
||
-
|
||
|
||
Suppose you are
|
||
|
||
-
|
||
|
||
Suppose you are
|
||
|
||
-
|
||
|
||
Suppose you are
|
||
|
||
exploring with Google
|
||
|
||
searching Google (web
|
||
|
||
shopping on Amazon
|
||
|
||
earth (desktop app),
|
||
|
||
app), you enter some
|
||
|
||
(mobile app), you tap
|
||
|
||
you click a button and
|
||
|
||
text, click the button
|
||
|
||
a button and
|
||
|
||
it
|
||
|
||
and
|
||
|
||
-
|
||
|
||
-
|
||
|
||
Closes and/or
|
||
|
||
refreshes, losing
|
||
|
||
program/window
|
||
|
||
your search text
|
||
|
||
Changes the size of
|
||
|
||
-
|
||
|
||
The result page
|
||
|
||
The app closes (and
|
||
maybe reopens)
|
||
|
||
The web page
|
||
|
||
reopens the
|
||
|
||
-
|
||
|
||
The entire style of
|
||
the app changes
|
||
|
||
-
|
||
|
||
GUI components move
|
||
|
||
the window
|
||
|
||
comes up, without
|
||
|
||
around the view
|
||
|
||
Moves GUI
|
||
|
||
results
|
||
|
||
(unexpectedly)
|
||
|
||
components around
|
||
the view
|
||
|
||
-
|
||
|
||
-
|
||
|
||
-
|
||
|
||
Does nothing!
|
||
|
||
-
|
||
|
||
Nothing happens!
|
||
|
||
-
|
||
|
||
Nothing happens!
|
||
|
||
CODE DEMO
|
||
- Demo exception handling concepts
|
||
in Eclipse!
|
||
|
||
DO YOU HAVE ANY
|
||
QUESTIONS?
|
||
|
||
THANK
|
||
YOU!
|
||
|
||
@
|
||
|
||
hend.alkittawi@utsa.edu
|
||
|
||
By Appointment
|
||
Online
|
||
|
||
|