Evacuation.java:58

Download Report

Transcript Evacuation.java:58

COP 3330
Notes 3/7
Today’s Topics
• Exceptions
• Abstract Classes
Exceptions
• Exceptions are abnormal events that occur
during execution
• An unhandled exception is considered to
be a runtime error
• Generating an exception is a
comparatively slow operation, so don't use
them to deal with normal events in
performance critical code!
Exceptions
• Exceptions in Java are objects that inherit
from Exception, which inherits from
Throwable
• Exceptions have some of useful methods
that can be called (most notably
printStackTrace)
Exception Handling
• try-catch: Use a try-catch construct to deal
with exceptions that you know how to cope
with
– Example:
try{
Scanner fin = new Scanner(new File("inventory.txt"));
// continue on and read the file
}catch(FileNotFoundException e) {
System.out.println("Could not read inventory.txt");
}
Exception Handling
• throws: Declare that your method throws an exception if
the exception needs to be handled elsewhere
• Example from Rational class
public int intValue() throws ArithmeticException
{
// This may generate a divide-by-zero
return numerator / denominator;
}
• Bad example
public static void main(String[] args)
throws Exception
Try-catch
• Syntax of try-catch:
try {
Action
} catch( ExceptionType identifier) {
Handler
}
Try-catch
• Multiple catch clauses are permissible
• Example
try{
Scanner fin = new Scanner(new File("input.txt"));
while(fin.hasNext()) {
System.out.println(fin.nextInt()/fin.nextInt());
}
}catch(FileNotFoundException e){
System.out.println("Could not find input.txt");
}catch(InputMismatchException e) {
System.out.println("File contained non-integers");
}catch(NoSuchElementException e){
System.out.println("File contained an odd # of ints");
}catch(ArithmeticException e){
System.out.println("Divide by zero");
}
Finally Clause
• The finally clause of a try-catch is
executed regardless of if there are
exceptions or not (even uncaught
exceptions)
Finally Clause
• Example
int a[] = new int[10];
Scanner fin = null;
try {
fin = new Scanner(new File("example.txt"));
for(int i=0;i<10; i++)
a[i] = fin.nextInt();
} catch(FileNotFoundException e) {
System.out.println("File not found");
} finally {
if(fin != null)
fin.close();
}
Exception Handling
• throws: Declare that your method throws an exception if
the exception needs to be handled elsewhere
• Example from Rational class
public int intValue() throws ArithmeticException
{
// This may generate a divide-by-zero
return numerator / denominator;
}
• Bad example (Legal code, just bad idea)
public static void main(String[] args)
throws Exception
Interpreting a Stack Trace
• Example (from a modified version of
assignment 2 solution)
Exception in thread "main" java.lang.NullPointerException
at evacuation.Evacuee.compareTo(Evacuee.java:76)
at evacuation.Evacuee.compareTo(Evacuee.java:1)
at java.util.PriorityQueue.fixUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at java.util.PriorityQueue.add(Unknown Source)
at evacuation.EvacueeQueue.add(EvacueeQueue.java:27)
at evacuation.Evacuation.main(Evacuation.java:58)
Interpreting a Stack Trace
• What that means is that the program
crashed when something was added to
the EvacueeQueue
• The problem occurred when the
EvacueeQueue tried to compare two
Evacuees
• The problem occurred when the
comparison between two Evacuee tried to
call a method on an Object that was null
Abstract Classes
• Abstract classes are part way between an
interface and class
• Abstract classes are classes so they’re
extended rather than implemented
• Abstract classes cannot be instantiated
– Invalid example: Number x = new Number();
– Valid example: Number x = new Integer(5);
Abstract Classes
• Abstract classes can have data members
(interfaces can only have static constants)
• Abstract classes can have fully fledged
methods
• Abstract classes can have abstract
methods
Abstract Methods
• Abstract methods do not have a body
• Abstract methods have only a prototype
• A class that extends an abstract class
MUST implement the abstract methods it
inherits
Abstract Classes
• Example
public abstract class GeometricObject{
private Color color;
public Color getColor(){
return color;
}
public void setColor(Color c){
color = c;
}
public abstract Rectangle getBoundingBox();
}