{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
Image of page 2
Throwing an Exception 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?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern