17 - Exceptions CS133: Developing Programming Principles...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Exceptions CS133: Developing Programming Principles Lecture 17 Exceptions and Files An exception is an unexpected, unusual, or erroneous occurrence during the execution of a computer program: • Dividing by Zero • Accessing invalid memory • File not found CS 133 Course Notes Lecture 17, Slide 2 1 2 Exceptions In Java In Java, an exception is represented by an object of type Exception: • thrown at the point where the exception occurs • may be caught elsewhere in the program: • ArithmeticException • ArrayIndexOutOfBoundsException • FileNotFoundException CS 133 Course Notes Lecture 17, Slide 3 Example // Print the sum of array elements public static void printTotal(int a) { int i = 0, n = 0; while (i < a.length) { n += a[i++]; This test } prevents us from accessing System.out.println(n); elements beyond } the end of the array. CS 133 Course Notes Lecture 17, Slide 4 3 4 Example // Print the sum of array elements public static void printTotal(int a) { int i = 0, n = 0; while (true) { n += a[i++]; } System.out.println(n); } Exception thrown when i equals array length Example public static void printTotal(int a) { int i = 0, n = 0; try { while (true) { n += a[i++]; } } catch (Exception e) { System.out.println(n); } } CS 133 Course Notes Lecture 17, Slide 6 CS 133 Course Notes Lecture 17, Slide 5 5 6 try-catch Syntax try { Code that might throw an exception } catch (Exception e) { Code to be performed if an exception is thrown in the try block } finally { Optional code to be executed regardless of whether an exception was thrown or not } Throwing an Exception An Exception may be thrown by the system itself (e.g., divide by zero) or by an explicit throw statement: throw new Exception-class-name (args) When an exception is thrown, control is transferred to an enclosing catch block CS 133 Course Notes Lecture 17, Slide 8 CS 133 Course Notes Lecture 17, Slide 7 7 8 Example public static void hello() { try { throw new Exception("Hi"); } catch (Exception e) { System.out.println("Hello world"); } } Exception Class Exception(String message) Constructs a new Exception with the specified message. public String getMessage() Returns the message String associated with this Exception. Subclasses of the Exception class usually define other constructors and methods. CS 133 Course Notes Lecture 17, Slide 9 CS 133 Course Notes Lecture 17, Slide 10 9 10 Example public static void hello() { try { throw new Exception("Hi"); } catch (Exception e) { System.out.println(e.getMessage()); } } throws Clause public class A { public static void a() throws Exception { throw new Exception("Hi"); } } In general, if a method can throw an Exception this possibility must be explicitly declared by a throws clause. CS 133 Course Notes Lecture 17, Slide 12 CS 133 Course Notes Lecture 17, Slide 11 11 12 throws Clause public class B { public static void b() throws Exception { A.a(); } } Exception Propagation public static void main(String args) { try { B.b(); } catch (Exception e) { System.out.println(e.getMessage()); } } Since b calls A.a(), which may throw an Exception, it requires a throws clause as well. CS 133 Course Notes Lecture 17, Slide 13 CS 133 Course Notes Lecture 17, Slide 14 13 14 Exception Propagation main(…) call An Uncaught Exception ArrayIndexOutOfBoundsException: 5 at ExceptionEg.printTotal(ExceptionEg.java:9) at ExceptionEg.main(ExceptionEg.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) Propagated to main and caught Propagated to b b() call a() Exception occurs in a If you don’t catch an Exception, it’s caught by Java. CS 133 Course Notes Lecture 17, Slide 16 CS 133 Course Notes Lecture 17, Slide 15 15 16 Files Why do we need to work with files? • Save data between executions of a program. • Work with more data than we have memory. • Move data from one machine to another. Streams In Java, all I/O is handled through streams. A stream is an object that delivers data to a destination or takes data from a source. e.g., System.out and Scanner CS 133 Course Notes Lecture 17, Slide 17 CS 133 Course Notes Lecture 17, Slide 18 17 18 File Types There are two major file types in Java: 1. Text files – lines of characters (e.g., email messages, Java code) 2. Binary files – sequence of ones and zeros (e.g., images, music, ) CS 133 Course Notes Lecture 17, Slide 19 File I/O in Java Every class which performs file I/O in Java must import the Java I/O package: import java.io.*; CS 133 Course Notes Lecture 17, Slide 20 19 20 Filenames Files are initially referenced (“opened”) using the naming convention appropriate to the operating system: C:\home\charles\cs133\Lecture-17.ppt /u/claclark/cs133/Lecture-17.ppt Filenames in Windows Due to limitations of DrJava on a Windows machine the complete pathname of files must be specified. C:\My Stuff\CS133\Assignment 2\out.txt Afterwards, a file is referenced by its associated stream object. This can be quite annoying, since paths can be long under Windows. When working on the Macs in the labs, complete pathnames are not required. CS 133 Course Notes Lecture 17, Slide 22 CS 133 Course Notes Lecture 17, Slide 21 21 22 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online