Download presentation
Presentation is loading. Please wait.
Published byShanon Aldous Ferguson Modified over 9 years ago
1
COP 3330 Notes 3/7
2
Today’s Topics Exceptions Abstract Classes
3
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!
4
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)
5
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"); }
6
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
7
Try-catch Syntax of try-catch: try { Action } catch( ExceptionType identifier) { Handler }
8
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"); }
9
Finally Clause The finally clause of a try-catch is executed regardless of if there are exceptions or not (even uncaught exceptions)
10
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(); }
11
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
12
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)
13
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
14
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);
15
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
16
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
17
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(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.