CPRG215 - Module 4 - Topic 1_v3_YD

Download Report

Transcript CPRG215 - Module 4 - Topic 1_v3_YD

CPRG 215
Introduction to
Object-Oriented Programming with Java
Module 4- Exception and Error Handling
Topic 4.1
Errors and Exceptions
Produced by Harvey Peters, 2008
Copyright SAIT
Please read the following sections
in your textbook
Core Java, Volume I–Fundamentals, Eighth Edition
By Cay S. Horstmann & Gary Cornell
•Chapter 11 - Exceptions, Logging, Assertions, and
Debugging
•Dealing with Errors
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Exceptions
• java.lang.Exception class defines “mild” error
conditions that your program encounters
• java.lang.Error class defines serious error
conditions
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Exception handling allows a program to “recover”
from exceptions
– When exception is encountered, an Exception object of the
type that occurred is generated and can be intercepted
– Rather than crashing the program, special handling can
allow the program to work around the problem
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Exceptions can occur when:
– you try to open a file that does not exist
– network connection is disrupted
– operands being manipulated are outside of
prescribed ranges
– the class file you are loading is missing
– Many more…
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Errors can occur when:
–
–
–
–
–
–
The system runs out of memory
The memory stack overflows
The virtual machine has an error
A method does not exist
A class definition does not exist
Many more…
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Common Exception classes
–
–
–
–
–
–
ArithmeticException
NullPointerException
NegativeArraySizeException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
SecurityException
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• When to use Exception handling
– Exception processing causes extra overhead,
reducing program efficiency
– If an Exception is regular, it is best to fix the program
to handle the situation without causing an exception
• Example: overflowing an Array – use the Array length
instead of a fixed number for loops
• Example: converting a number that was entered in a
text field by the user – validate that the value contains
only digits before converting
– If the Exception is random or unpredictable, trap
Exceptions and handle them
• Example: a file does not exist, the network connection
is down, the database connection failed
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Example:
Check example:
Exceptionexample.java
public class HelloWorld {
public static void main (String args[ ]) {
int i = 0;
String greetings [ ] = { “Hi!”, ”Yo!”, “Hey!” };
while (i < 4) {
System.out.println (greetings[i]);
i++;
}
}
}
- this should cause an exception as I moves out
of the array bounds
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1
Exceptions
• Built-in methods often throw exceptions
– Called the “Declare” approach
– Java API documentation for Integer class parseInt()
method
• public static int parseInt(String s) throws
NumberFormatException
– “throws” indicates that the method can produce this
kind of exception
– By declaring that the method throws the exception, it
will be passed from the method to the calling point to
be processed
– If it is not handled in the program it causes the
program to exit and the JVM handles it
• We will examine the “Handle” approach in the
next topic
CPRG 215 Module 4.1- Errors and Exceptions
Copyright SAIT
Topic 4.1.1