lec16 - 1.00 Lecture 16 Exceptions Nested and Inner Classes...

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

View Full Document Right Arrow Icon
1.00 Lecture 16 Exceptions Nested and Inner Classes Reading for next time: Big Java: sections 4.1-4.10 Exceptions: Try, throw, catch Exceptions are how Java handles errors that the method in which the error occurs can’t handle The Java exception mechanism has three elements: Throw (what a method does) Method detects error, cannot handle it Method throws an exception Try block (what the caller of the method does first) Method calls that may throw an exception are placed in a try block (defined by curly braces) in the calling method Catch blocks follow try blocks (what caller does second) Each block contains an exception handler of a given type 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Catching an exception import javax.swing.*; public class BadInput { public static void main(String[] args) { while (true) { String answer = JOptionPane.showInputDialog("Enter an integer (0 to quit)"); int intAnswer = -1; try { // Try block intAnswer = Integer.parseInt(answer); // Throw } catch (NumberFormatException e) { // Catch block JOptionPane.showMessageDialog(null, "Not an integer"); } if (intAnswer == 0) break; } System.exit(0); } } Exercise Download BadInput from the Web site Comment out: Try block (‘try’ and the curly braces; leave intAnswer = …), Catch block (remove the entire block, including code) Save/compile Enter non-integer input. See what happens. What happens if the user types a non-integer, Cathy, for example? Is this what we’ve been doing so far in 1.00 for input? Then remove the comments, restoring the try/catch blocks Save/compile Enter non-integer input. What happens? Is this better? 2
Background image of page 2
public static double average(double[] dArray) throws IllegalArgumentException { if (dArray.length == 0) throw new IllegalArgumentException(); // Exceptions are objects! double sum = 0.0; for (int i = 0; i < dArray.length; i++) sum += dArray[i]; return sum / dArray.length; } Exercise Download class AverageTest, which has: The average() method from the previous slide A partially written main() method Creates nonzero-length array a Creates zero length array b as: double[] b= { }; Complete the main() method in AverageTest that calls the average() method: Call average() twice, with the zero-length and the nonzero-length arrays Put the average() calls in a try block and catch the exceptions Save/compile and run. What happens? 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 12

lec16 - 1.00 Lecture 16 Exceptions Nested and Inner Classes...

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

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