Lecture17

Lecture17 - Chapter 11: Exceptions An exception is an event...

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

View Full Document Right Arrow Icon
1 Chapter 11: Exceptions An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions The Java language uses exceptions to provide error-handling capabilities for its programs. Exceptions are represented as classes in Java. (Hardware or System) Throwable Error Exception LinkageError VirtualMachineErr or RunTimeException ArithmeticException IndexOutOfBoundsException IllegalAccessException NegativeArraySizeException
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 What happens after an exception occurs. How is the exception handled? A program can deal with an exception in one of three ways: ignore it handle it where it occurs handle it an another place in the program Exception: Ignore It The program will terminate and produce an appropriate message. public class Zero { public static void main (String[] args) { int numerator = 10; int denominator = 0; System.out.println (numerator / denominator); System.out.println ("This text will not be printed."); }}
Background image of page 2
3 Exception: Handle Where It Occurs Exception is represented as an object in Java. They are thrown by a program, and may be caught and handled by another part of the program
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 The try and catch Statement To process an exception when it occurs, the line that throws the exception is executed within a try block A try block is followed by one or more catch clauses, which contain code to process an exception Each catch clause has an associated exception type. When an exception occurs, processing continues at the first catch clause that matches the exception type try { // Code that might generate exceptions } catch (Type1 id1) { // Handle exceptions of Type1 } catch (Type2 id2) { // Handle exceptions of Type2 } catch (Type3 id3) { // Handle exceptions of Type3 } // etc. ..
Background image of page 4
5 public class ProductCodes{ // Counts the number of product codes that are // entered with a zone of R and district greater than 2000. public static void main (String[] args)
Background image of page 5

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

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

Page1 / 18

Lecture17 - Chapter 11: Exceptions An exception is an event...

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

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