Week 10 - Exceptions
Week 10 - Exceptions
Exceptions
John Lewis
William Loftus
Exception Handling
The try-catch Statement
Exception Classes
I/O Exceptions
• See Zero.java
Exception Handling
The try-catch Statement
Exception Classes
I/O Exceptions
• See ProductCodes.java
Copyright © 2012 Pearson Education, Inc.
Sample Run
Enter product code (XXX to quit): TRV2475A5R-14
Enter product code (XXX to quit): TRD1704A7R-12
Enter product code (XXX to quit): TRL2k74A5R-11
District is not numeric: TRL2k74A5R-11
Enter product code (XXX to quit): TRQ2949A6M-04
Enter product code (XXX to quit): TRV2105A2
Improper code length: TRV2105A2
Enter product code (XXX to quit): TRQ2778A7R-19
Enter product code (XXX to quit): XXX
# of valid codes entered: 4
# of banned codes entered: 2
import java.util.Scanner;
continue
try
{
level2();
}
catch (ArithmeticException problem)
{
System.out.println ();
System.out.println ("The exception message is: " +
problem.getMessage());
System.out.println ();
continue
System.out.println("Level 1 ending.");
}
//-----------------------------------------------------------------
// Serves as an intermediate level. The exception propagates
// through this method back to level1.
//-----------------------------------------------------------------
public void level2()
{
System.out.println("Level 2 beginning.");
level3 ();
System.out.println("Level 2 ending.");
}
continue
//-----------------------------------------------------------------
// Performs a calculation to produce an exception. It is not
// caught and handled at this level.
//-----------------------------------------------------------------
public void level3 ()
{
int numerator = 10, denominator = 0;
System.out.println("Level 3 beginning.");
int result = numerator / denominator;
System.out.println("Level 3 ending.");
}
}
System.out.println("Program beginning.");
demo.level1();
System.out.println("Program ending.");
}
}
Exception Handling
The try-catch Statement
Exception Classes
I/O Exceptions
NullPointerException
IndexOutOfBoundsException
ClassNotFoundException
NoSuchMethodException
ArithmeticException
NullPointerException Unchecked
IndexOutOfBoundsException Unchecked
ClassNotFoundException Checked
NoSuchMethodException Checked
ArithmeticException Unchecked
• See CreatingExceptions.java
• See OutOfRangeException.java
OutOfRangeException
import java.util.Scanner;
OutOfRangeException problem =
new OutOfRangeException ("Input value is out of range.");
continue
System.out.println("Before throw");
throw new OutOfRangeException("Too High");
System.out.println("After throw");
System.out.println("Before throw");
throw new OutOfRangeException("Too High");
System.out.println("After throw");
Exception Handling
The try-catch Statement
Exception Classes
I/O Exceptions
import java.util.Random;
import java.io.*;
int value;
String file = "test.data";
continue
outFile.close();
System.out.println ("Output file has been created: " + file);
}
}
45 26 47 68 55 98 34 38 98 38
48 59 90 12 86 36 11 65 41 62
class throws1 {
void show() {
try {
throw new Exception();
} catch(Exception e) {
// code to handle the exception
}
}
}
Summary
• Chapter 11 has focused on:
• the purpose of exceptions
• exception messages
• the try-catch statement
• propagating exceptions
• the exception class hierarchy