Slides0213 - COP 3330 Slides 2/13 Topics Exceptions...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Slides 2/13 COP 3330
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Exceptions Interfaces
Background image of page 2
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!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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)
Background image of page 4
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"); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
Try-catch Syntax of try-catch: try { Action } catch( ExceptionType identifier ) { Handler }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Try-catch
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/09/2010 for the course CS COP 3330 taught by Professor Douglass during the Spring '09 term at University of Central Florida.

Page1 / 23

Slides0213 - COP 3330 Slides 2/13 Topics Exceptions...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online