Object Oriented Programming - Chapter 4 - Excption Handling
Object Oriented Programming - Chapter 4 - Excption Handling
Exceptions Handling
Compiled by Hashim S.
Exceptions
An exception is an object that describes an unusual or
erroneous situation
Exceptions are thrown by a program, and may be caught and
handled by another part of the program
Exception Hierarchy
Errors:
Errors are fairly rare and usually fatal. They are caused by
bugs in the Java VM, or by the program running out of
memory or other resources.
Errors are usually not handled by programs.
Exceptions:
Exceptions can be caused by bugs in the program or improper
data supplied to the program
Exceptions should be handled, or caught
An exception is either checked or unchecked
Checked exceptions must be fixed or declared as thrown.
You are not required to handle unchecked exceptions.
Attack of the Exception
public static int average(int[] a) {
int total = 0;
int total1=0;
for(int i = 0; i < a.length; i++) {
total += a[i];
}
return total /total1;
}
What happens when this method is used to take the average of
an array of length zero?
Program throws an Exception and fails
java.lang.ArithmeticException: / by
zero
What is an Exception?
An error event that disrupts the program flow and may
cause a program to fail.
Some examples:
Performing illegal arithmetic
Illegal arguments to methods
Accessing an out-of-bounds array element
Hardware failures
Writing to a read-only file
Another Exception Example
What is the output of this program?
public class ExceptionExample {
public static void main(String args[]) {
String[] greek = {"Alpha", "Beta"};
System.out.println(greek[2]);
}
}
Output:
Exception in thread "main“ java.lang.ArrayIndexOutOfBoundsException: 2
at ExceptionExample.main(ExceptionExample.java:4)
Exception Message Details
Exception message format:
[exception class]: [additional description of exception]
at [class].[method]([file]:[line number])
Example:
java.lang.ArrayIndexOutOfBoundsException: 2
at ExceptionExample.main(ExceptionExample.java:4)
What exception class? ArrayIndexOutOfBoundsException
Which array index is out of bounds? 2
What method throws the exception? main
What file contains the method? ExceptionExample.java
What line of the file throws the exception? 4
Classifying Java Exceptions
Unchecked Exceptions
All the exceptions we've seen so far have been Unchecked
Exceptions, or Runtime Exceptions
Usually occur because of programming errors, when code is
not robust enough to prevent them
They are numerous and can be ignored by the programmer
Common Unchecked Exceptions
NullPointerException: reference is null and should not be
IllegalArgumentException: method argument is improper is
some way
IllegalStateException: method called when class is in improper
state
8
Checked Exceptions
Usually occur because of errors programmer cannot control
examples: hardware failures, unreadable files
They are less frequent and they cannot be ignored by the
programmer . . .
Every method must catch (handle) checked exceptions or
specify that it may throw them
Specify with the throws keyword
Checked and Unchecked Exceptions
Checked Exception Unchecked Exception
ArrayIndexOutofBounds FileNotFoundException
NullPointerException MalformedURLException
IllegalArgumentException SocketException
etc. etc.
12
Exceptions Terminology
When an exception happens we say it was thrown or
raised
When an exception is dealt with, we say the exception is
handled or caught
Exception Handling
Use a try-catch block to handle exceptions that are thrown
try {
// code that might throw exception
}
catch ([Type of Exception] e) {
// what to do if exception is thrown
}
Exception Handling Example
public static int average(int[] a) {
int total = 0;
int tot1=0;
for(int i = 0; i < a.length; i++) {
total += a[i];
}
return total / tot1;
}